如何在按钮单击android上获取Twitter趋势主题-twitter4j?

时间:2019-01-22 12:31:10

标签: java android twitter twitter4j

我一直在研究android一段时间,我正在尝试将一些twitter功能添加到我的应用中。

我想做的是获取Twitter趋势主题并将其放在String上,然后在textview上显示它们。可能吗?我在互联网上几乎找不到任何东西,因此希望您能为我提供帮助。

`

 @Override
        protected void onCreate(Bundle savedInstanceState)
       {
         super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_trending);


    getTrendingButton = (Button) findViewById(R.id.gettrendingbutton);
    trendingList = (TextView) findViewById(R.id.trendinglist);



    cb = new ConfigurationBuilder();
    cb.setApplicationOnlyAuthEnabled(false);
    cb.setOAuthConsumerKey("key")
            .setOAuthConsumerSecret("key")
            .setOAuthAccessToken("key")
            .setOAuthAccessTokenSecret("key");

    twitterFactory = new TwitterFactory(cb.build());
    twitter = twitterFactory.getInstance();


}

@Override
protected void onStart()
{
    super.onStart();

    getTrendingButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            String trendsString = " ";
            Trends trends;
            try {
                trends = twitter.getPlaceTrends(1);
                int count = 0;
                for (Trend trend : trends.getTrends()) {
                    if (count < 10)
                    {
                        trendsString = trend.getName().toString() + "\n";
                    }
                    trendingList.setText(trendsString);
                }
            } catch (TwitterException e) {
                e.printStackTrace();
            }

        }
    });


}`

这里是分析堆栈跟踪

  

android.os.NetworkOnMainThreadException                         在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)                         在java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)                         在java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)                         在java.net.InetAddress.getAllByName(InetAddress.java:787)                         在com.android.okhttp.Dns $ 1.lookup(Dns.java:39)                         在com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:200)                         在com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:148)                         在com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:90)                         在com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:190)                         在com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:142)                         在com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:104)                         在com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:392)                         在com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:325)                         在com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:489)                         在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:435)                         在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:566)                         在com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)                         在com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(未知来源:0)                         在twitter4j.HttpResponseImpl。(HttpResponseImpl.java:35)                         在twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:149)                         在twitter4j.HttpClientBase.request(HttpClientBase.java:57)                         在twitter4j.HttpClientBase.get(HttpClientBase.java:75)                         在twitter4j.TwitterImpl.get(TwitterImpl.java:2066)                         在twitter4j.TwitterImpl.getPlaceTrends(TwitterImpl.java:1912)                         在gusmappdev.myapp.TrendingActivity $ 1.onClick(TrendingActivity.java:77)                         在android.view.View.performClick(View.java:6897)                         在android.widget.TextView.performClick(TextView.java:12693)                         在android.view.View $ PerformClick.run(View.java:26100)                         在android.os.Handler.handleCallback(Handler.java:789)                         在android.os.Handler.dispatchMessage(Handler.java:98)                         在android.os.Looper.loop(Looper.java:164)                         在android.app.ActivityThread.main(ActivityThread.java:6942)                         在java.lang.reflect.Method.invoke(本机方法)                         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:327)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

希望你们能帮助我:) 谢谢

2 个答案:

答案 0 :(得分:0)

您必须在另一个线程中进行网络访问。不允许在主应用程序线程中进行网络调用。这就是为什么您得到NetworkOnMainThreadException的原因。

下面是解决此问题的示例:How do I fix android.os.NetworkOnMainThreadException?

答案 1 :(得分:0)

有一种在getPlaceTrends()实例中使用AsyncTwitter方法的方法。但是,在这种情况下,方法是void,我不确定如何存储结果。