我一直在研究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();
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 at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet .java:90),位于com.android.okhttp.Dns $ 1.lookup(Dns.java:39),位于com.android.okhttp.internal.http.RouteSelector的java.net.InetAddress.getAllByName(InetAddress.java:787) com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:148)的.resetNextInetSocketAddress(RouteSelector.java:200)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的。 com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:39上的internal.http.StreamAllocation.newStream(StreamAllocation.java:104) 2)在com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:325)在com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:489)在com.android.okhttp com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:566)的com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode的.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:435) (com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(DelegatingHttpsURLConnection.java:105)at twitter4j.HttpResponseImpl。(HttpResponseImpl.java:35)at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java: 149),位于twitter4j.TwitterImpl.getPlaceTrends(TwitterImpl.twitter4j.TwitterImpl.get(TwitterImpl.java:2066)的twitter4j.HttpClientBase.get(HttpClientBase.java:75)处的twitter4j.HttpClientBase.request(HttpClientBase.java:75)处。 java:1912)at gusmappdev.myapp.TrendingActivity $ 1.onClick(T rendingActivity.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 .com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:327)的java.lang.reflect.Method.invoke(本机方法)的.app.ActivityThread.main(ActivityThread.java:6942) .android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
希望你们能帮助我:)谢谢