您应该使用AsyncTask或其他任何库,但由于您是初学者,所以我建议您暂时使用asynchtask。我提供了解决方案,但是您必须了解AsyncTask是如何工作的
public class FetchData extends AsyncTask<Void,Void,List>{
@Override
protected void onPreExecute() {
super.onPreExecute();
// this will run first and you can perform any ui based task like showing progress bar here
webView=(TextView) findViewById(R.id.webview);
}
@Override
protected List doInBackground(Void... voids) {
// this will run after pro execute and it will run on another thread and you can not perform any ui based task here
ConfigurationBuilder cf = new ConfigurationBuilder();
cf.setDebugEnabled(true)
.setOAuthConsumerKey("YYOkYp9G85lFxfXZPQAWcjAmX")
.setOAuthConsumerSecret("TLZrCAgeiuCbN6WOoUzO6e0hfWySA01PEOK1KPEH6yB8kxEYSz ")
.setOAuthAccessToken("4188976763-zVUUUMwEKvBD6J9mE3tiscaKQD85vWRkIyPdqBl")
.setOAuthAccessTokenSecret("K1neDCJq9shWhBxxU6otD4PCU4HJObd8lch3X4XsRq5ew ");
TwitterFactory tf = new TwitterFactory(cf.build());
twitter4j.Twitter twitter = tf.getInstance();
Log.d(TAG, "main: tweets messages:");
int totalTweets = 1 ;// no of tweets to be fetched
Paging paging = new Paging(1, totalTweets);
List tweets = twitter.getHomeTimeline(paging);
return tweets;
}
@Override
protected void onPostExecute(List result) {
super.onPostExecute(result);
// this will run at last after doInBackground and you can perform any ui based task like showing progress bar here
webView.setText(result.toString());
}
}
lastTwitterpost()
打电话给
new FetchData().execute()