twitter4j在搜索twitter时出错

时间:2015-09-01 13:44:26

标签: android twitter4j

[编辑] 我正在尝试使用twitter4j版本4.0.4作为库从Twitter搜索一些推文。这是我的方法

public void processAll(View view){
    Log.d("click","OKE");
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
            .setOAuthConsumerKey("xxxxx")
            .setOAuthConsumerSecret("xxxx")
            .setOAuthAccessToken("7xxxxL")
            .setOAuthAccessTokenSecret("xxxxx");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    Log.d("connected","OKE");

    try {
        Query query = new Query("handsome");
        Log.d("TEST","OKE");
        QueryResult result = twitter.search(query);
        List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
            Log.d("RESULT","@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
        }
    }
    catch (TwitterException te) {
        te.printStackTrace();
        Log.d("RESULT","Failed to search tweets: " + te.getMessage());
        return;
    }

}

但是在调用此方法时我的应用程序停止了。

 at com.fajarainul.coconut_dev.titikota.SetTimeActivity.processAll(SetTimeActivity.java:68)

的引用
    QueryResult result = twitter.search(query);

这里是我的完整logcat

09-01 21:17:52.862  12562-12562/com.fajarainul.coconut_dev.titikota E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
09-01 21:17:53.947  12562-12562/com.fajarainul.coconut_dev.titikota E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.fajarainul.coconut_dev.titikota, PID: 12562
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3834)
            at android.view.View.performClick(View.java:4461)
            at android.view.View$PerformClick.run(View.java:18523)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5118)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3829)
            at android.view.View.performClick(View.java:4461)
            at android.view.View$PerformClick.run(View.java:18523)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5118)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1148)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:251)
            at java.net.InetAddress.getAllByName(InetAddress.java:229)
            at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
            at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
            at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
            at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
            at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
            at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
            at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:136)
            at twitter4j.HttpResponseImpl.<init>(HttpResponseImpl.java:35)
            at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:142)
            at twitter4j.HttpClientBase.request(HttpClientBase.java:53)
            at twitter4j.HttpClientBase.get(HttpClientBase.java:71)
            at twitter4j.TwitterImpl.get(TwitterImpl.java:1556)
            at twitter4j.TwitterImpl.search(TwitterImpl.java:247)
            at com.fajarainul.coconut_dev.titikota.SetTimeActivity.processAll(SetTimeActivity.java:70)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3829)
            at android.view.View.performClick(View.java:4461)
            at android.view.View$PerformClick.run(View.java:18523)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5118)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
            at dalvik.system.NativeStart.main(Native Method)

提前致谢!

1 个答案:

答案 0 :(得分:2)

你正在抛出一个NetworkOnMainThreadException,这意味着你试图在应用程序主线程上运行网络代码(即通过twitter API运行搜索)。

您需要使用AsyncTask之类的内容(请参阅docs)将此代码移动到后台线程中。

我建议阅读文档以及对上一个问题的回答:How to fix android.os.NetworkOnMainThreadException?