android - Publishing last tweet with twitter4j library - where exactly AsyncTask should be implemented?
问题描述
My code is simple: retreiving the last tweet from my home page and publishing in Textview field in my app.
public void lastTwitterpost() throws TwitterException
{
webView=(TextView) findViewById(R.id.webview);
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);
webView.setText(tweets.toString());
}
But i get follow error:
FATAL EXCEPTION: main Process: com.example.android.bluesky, PID: 8320
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.bluesky/com.example.android.bluesky.Search.SearchActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2406)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
at android.app.ActivityThread.access$1200(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
How can i handle it?
解决方案
您应该使用 AsyncTask 或不使用其他库,但由于您是初学者,所以我现在建议您使用 asynch 任务。我正在提供解决方案,但您必须了解 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()
推荐阅读
- c - 错误:核心转储在获取 C 中的字符串时
- python - 用替换字符串列表中的下一项替换匹配项
- cloudinary - 如何从 Cloudinary JAVA 的 API 响应对象中检索 URL
- python - TWS API - 存储公司快照和财务报表
- javascript - 用 webpacker 理解 Rails 和 JS 的发生率
- php - 尝试使用搜索表单显示产品数据库中的结果
- python - TypeError:lemmatize() 缺少 3 个必需的位置参数:'index'、'exceptions' 和 'rules'
- postgresql - 在 Heroku 上托管 RESTful API
- java - Firebase 数据库只更新一个数据
- django - django_heroku.settings(locals()) 不在 collectstatic 的末尾