简体   繁体   English

Android4上带有回调URL的Twitter4j getOAuthRequestToken()失败了吗?

[英]Twitter4j getOAuthRequestToken() with callback URL fails on Android?

I am trying to integrate Twitter into my Android Application and can't seem to get Twitter4j to register my app. 我正在尝试将Twitter集成到我的Android应用程序中,似乎无法让Twitter4j注册我的应用程序。

Every tutorial and question regarding the issue I've seen for Android have been years old and things seem to have moved on since then. 关于我在Android上看到过的问题的每个教程和问题都已经存在了很多年,从那以后事情似乎已经发生了变化。

I followed the steps on this tutorial and used the supplied source code to test connecting to Twitter. 我按照本教程中的步骤操作,并使用提供的源代码测试连接到Twitter。 Knowing that the Twitter4j version supplied with the source code was out of date I updated it the latest version and snapshot version later on, but all to no avail. 知道随源代码提供的Twitter4j版本已经过时了,我后来更新了最新版本和快照版本,但都无济于事。

private void loginNewUser() {
    try {
        Log.i(TAG, "Request App Authentication");

        // This line fails with error "Host is unresolved: api.twitter.com:80
        mReqToken = mTwitter.getOAuthRequestToken(); 

        Log.i(TAG, "Starting Webview to login to twitter");
        WebView twitterSite = new WebView(this);
        twitterSite.loadUrl(mReqToken.getAuthenticationURL());
        setContentView(twitterSite);

    } catch (TwitterException e) {
        Log.e("errors", e.getMessage());
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

Of anyone has any working login code using Twitter4j or knows a solution to the problem I'd be grateful to hear from you! 任何人都有使用Twitter4j的任何工作登录代码或知道问题的解决方案我将不胜感激收到你的消息!

Okay I figured it out and I hope anyone who comes across the same problem doesn't have to suffer as I did. 好吧,我想通了,我希望遇到同样问题的人不必像我一样受苦。

1) The error "Host is unresolved: api.twitter.com:80" comes from the fact that the device did not have a WiFi signal or 3G connectivity (The WiFi is a bit flaky in the office I work in). 1)错误“主机未解决:api.twitter.com:80”来自于设备没有WiFi信号或3G连接(在我工作的办公室中WiFi有点不稳定)。

2) getOAuthRequestToken() crashes in any Android version greater than 3.0 Honeycomb because HTTP requests need to be done on a separate thread from the UI thread. 2) getOAuthRequestToken()在任何大于3.0 Honeycomb的Android版本中崩溃,因为HTTP请求需要在与UI线程不同的线程上完成。 I had this problem because I had set the target version in my Android manifest to 4.0 Ice Cream Sandwich. 我遇到了这个问题,因为我已将Android清单中的目标版本设置为4.0 Ice Cream Sandwich。

I had this problem too and the reason I couldn't log in to Twitter is that I modified the time and date of my device for making tests. 我也有这个问题,我无法登录Twitter的原因是我修改了我的设备进行测试的时间和日期。

Once that I put the right date and time, the login to Twitter worked. 一旦我把正确的日期和时间,登录Twitter工作。

Hope it helps anybody. 希望它可以帮助任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM