使用Twitter4J的Twitter搜索方法

时间:2013-11-14 23:27:46

标签: java android eclipse twitter4j

我正在做一个应用程序,你写一个单词,一个新的活动打开然后所有的推文都应该出现,但我收到了错误。

我在新活动的主要内部调用方法printTweets

并且不用担心我的所有凭据都有效,我已经在java应用程序(不是android)上尝试了代码,它运行良好,我得到了结果。

这是我的代码:

    public void printTweets(String q) {
    LinearLayout layout = new LinearLayout(this);
    setContentView(layout);
    layout.setOrientation(LinearLayout.VERTICAL);

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey("xxxxx")
      .setOAuthConsumerSecret("xxxxxx")
      .setOAuthAccessToken("xxxxxx")
      .setOAuthAccessTokenSecret("xxxxx");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();

        Query query = new Query(q);
        QueryResult result = null;
        do{
            try {
                result = twitter.search(query);
            } catch (TwitterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
            TextView tv=new TextView(getApplicationContext());
            tv.setText("@" + tweet.getUser().getScreenName() + ":" + tweet.getText());
            layout.addView(tv);

        }
        } while ((query = result.nextQuery()) != null);

}

这是我的错误:

  

11-15 01:07:30.890:E / AndroidRuntime(12926):致命异常:主要   11-15 01:07:30.890:E / AndroidRuntime(12926):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.sentimentmining / com.example.sentimentmining.DisplayResults}:android.os.NetworkOnMainThreadException   11-15 01:07:30.890:E / AndroidRuntime(12926):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)   11-15 01:07:30.890:E / AndroidRuntime(12926):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)   11-15 01:07:30.890:E / AndroidRuntime(12926):在android.app.ActivityThread.access $ 600(ActivityThread.java:128)   11-15 01:07:30.890:E / AndroidRuntime(12926):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1161)   11-15 01:07:30.890:E / AndroidRuntime(12926):在android.os.Handler.dispatchMessage(Handler.java:99)   11-15 01:07:30.890:E / AndroidRuntime(12926):在android.os.Looper.loop(Looper.java:137)   11-15 01:07:30.890:E / AndroidRuntime(12926):在android.app.ActivityThread.main(ActivityThread.java:4514)   11-15 01:07:30.890:E / AndroidRuntime(12926):at java.lang.reflect.Method.invokeNative(Native Method)   11-15 01:07:30.890:E / AndroidRuntime(12926):at java.lang.reflect.Method.invoke(Method.java:511)   11-15 01:07:30.890:E / AndroidRuntime(12926):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790)   11-15 01:07:30.890:E / AndroidRuntime(12926):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)   11-15 01:07:30.890:E / AndroidRuntime(12926):at dalvik.system.NativeStart.main(Native Method)   11-15 01:07:30.890:E / AndroidRuntime(12926):引起:android.os.NetworkOnMainThreadException   11-15 01:07:30.890:E / AndroidRuntime(12926):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)   11-15 01:07:30.890:E / AndroidRuntime(12926):at java.net.InetAddress.lookupHostByName(InetAddress.java:391)   11-15 01:07:30.890:E / AndroidRuntime(12926):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)   11-15 01:07:30.890:E / AndroidRuntime(12926):at java.net.InetAddress.getAllByName(InetAddress.java:220)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpConnection。(HttpConnection.java:71)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpConnection。(HttpConnection.java:50)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:351)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.connect(HttpsURLConnectionImpl.java:432)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)   11-15 01:07:30.890:E / AndroidRuntime(12926):at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:133)   11-15 01:07:30.890:E / AndroidRuntime(12926):在twitter4j.internal.http.HttpResponseImpl。(HttpResponseImpl.java:34)   11-15 01:07:30.890:E / AndroidRuntime(12926):在twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:141)   11-15 01:07:30.890:E / AndroidRuntime(12926):在twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)   11-15 01:07:30.890:E / AndroidRuntime(12926):在twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:81)   11-15 01:07:30.890:E / AndroidRuntime(12926):在twitter4j.TwitterImpl.get(TwitterImpl.java:1929)   11-15 01:07:30.890:E / AndroidRuntime(12926):在twitter4j.TwitterImpl.search(TwitterImpl.java:306)   11-15 01:07:30.890:E / AndroidRuntime(12926):at com.example.sentimentmining.DisplayResults.printTweets(DisplayResults.java:83)   11-15 01:07:30.890:E / AndroidRuntime(12926):at com.example.sentimentmining.DisplayResults.onCreate(DisplayResults.java:34)   11-15 01:07:30.890:E / AndroidRuntime(12926):在android.app.Activity.performCreate(Activity.java:4465)   11-15 01:07:30.890:E / AndroidRuntime(12926):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)   11-15 01:07:30.890:E / AndroidRuntime(12926):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)   11-15 01:07:30.890:E / AndroidRuntime(12926):... 11更多

如果您还需要更多信息,请告诉我,我会把它给你

1 个答案:

答案 0 :(得分:0)

NetworkOnMainThreadException

在清单

中设置此权限
 <uses-permission android:name="android.permission.INTERNET" />

使用asyntask

您获得此异常的原因是您尝试在UI线程上执行昂贵的操作,这会显着减慢您的应用程序并导致其强行关闭。您应该将代码包装在AsyncTask(或Thread)中。

在进行网络操作之前​​使用它

if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }