Twitter与Android集成

时间:2014-06-24 03:39:14

标签: android twitter twitter4j

我正在尝试将Twitter与Android中的应用集成。我正在尝试http://goo.gl/VF4Pk

中给出的androidHive指南

每次点击登录Twitter按钮时,我都会收到以下错误。

06-24 08:58:09.659: E/AndroidRuntime(30667): FATAL EXCEPTION: main
06-24 08:58:09.659: E/AndroidRuntime(30667): Process: com.example.testoauthtweets, PID: 30667
06-24 08:58:09.659: E/AndroidRuntime(30667): android.os.NetworkOnMainThreadException
06-24 08:58:09.659: E/AndroidRuntime(30667):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:150)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:98)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:122)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:281)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.example.testoauthtweets.MainActivity.loginToTwitter(MainActivity.java:189)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.example.testoauthtweets.MainActivity.access$1(MainActivity.java:175)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.example.testoauthtweets.MainActivity$1.onClick(MainActivity.java:123)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at android.view.View.performClick(View.java:4438)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at android.view.View$PerformClick.run(View.java:18422)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at android.os.Handler.handleCallback(Handler.java:733) 
06-24 08:58:09.659: E/AndroidRuntime(30667):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at android.os.Looper.loop(Looper.java:136)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at android.app.ActivityThread.main(ActivityThread.java:5001)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at java.lang.reflect.Method.invoke(Native Method)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-24 08:58:09.659: E/AndroidRuntime(30667):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

我收到错误:

    private void loginToTwitter() {
    // Check if already logged in
    if (!isTwitterLoggedInAlready()) 
    {
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
        builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
        Configuration configuration = builder.build();

        TwitterFactory factory = new TwitterFactory(configuration);
        twitter = factory.getInstance();

        try {
            requestToken = twitter
                    .getOAuthRequestToken(TWITTER_CALLBACK_URL);
            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                    .parse(requestToken.getAuthenticationURL())));
        } catch (TwitterException e) {
            e.printStackTrace();
        }
    } 

    else 
    {
        // user already logged into twitter
        Toast.makeText(getApplicationContext(),
                "Already Logged into twitter", Toast.LENGTH_LONG).show();
    }
}

更新1:

我将库文件更新为最新版本。现在我能够在登录后获得登录屏幕并接受它返回到我的应用程序的访问权限,但随后它总是显示登录错误 - NULL 。看起来访问令牌没有得到更新或接受。

有人可以帮我吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

  

android.os.NetworkOnMainThreadException

您正在UI(主)线程中执行与网络相关的操作。您必须在单独的工作线程中执行此操作或使用asynctask。

NetworkOnMainThreadException - Developers site

注意:仅针对Honeycomb SDK或更高版本的应用程序抛出此内容。

更新1: 在你的情况下,在asnctask的doInBackground方法中输入用于获取请求令牌和获取认证URL的代码。

答案 1 :(得分:0)

如果有效,请试试这个:

private void loginToTwitter() {
// Check if already logged in
if (!isTwitterLoggedInAlready()) 
{
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
    builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
    Configuration configuration = builder.build();

    TwitterFactory factory = new TwitterFactory(configuration);
    twitter = factory.getInstance();

    try {

        new Thread(new Runnable() {

            @Override
            public void run() {
                requestToken = twitter
                        .getOAuthRequestToken(TWITTER_CALLBACK_URL);

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                         this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                                    .parse(requestToken.getAuthenticationURL())));
                    }
                });

            }
        }).start();



    } catch (TwitterException e) {
        e.printStackTrace();
    }
} 

else 
{
    // user already logged into twitter
    Toast.makeText(getApplicationContext(),
            "Already Logged into twitter", Toast.LENGTH_LONG).show();
}
}