我只是新來的android開發,我正在創建一個應用程序,它使用Twitter API來獲取一個實時推送供稿。然而,當我點擊btn_twitter_stream
應用程序崩潰,我得到休耕錯誤在我的logcat AsyncTask #1 java.lang.RuntimeException: An error occurred while executing doInBackground()
AsyncTask#1 java.lang.RuntimeException:執行doInBackground()時發生錯誤
logcat的
02-05 13:12:37.445 11097-11204/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:361)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
at saveourcar.text.twitter.BasicRestClient.request(BasicRestClient.java:81)
at saveourcar.text.twitter.TwitterRestClient.request(TwitterRestClient.java:17)
at saveourcar.text.twitter.BasicRestClient.postSync(BasicRestClient.java:53)
at saveourcar.text.twitter.TwitterRestClient.getBearerToken(TwitterRestClient.java:104)
at saveourcar.text.twitter.TwitterRestClient$1.doInBackground(TwitterRestClient.java:56)
at saveourcar.text.twitter.TwitterRestClient$1.doInBackground(TwitterRestClient.java:43)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
at libcore.io.Posix.getaddrinfo(Native Method)
at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:361)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
at saveourcar.text.twitter.BasicRestClient.request(BasicRestClient.java:81)
at saveourcar.text.twitter.TwitterRestClient.request(TwitterRestClient.java:17)
at saveourcar.text.twitter.BasicRestClient.postSync(BasicRestClient.java:53)
at saveourcar.text.twitter.TwitterRestClient.getBearerToken(TwitterRestClient.java:104)
at saveourcar.text.twitter.TwitterRestClient$1.doInBackground(TwitterRestClient.java:56)
at saveourcar.text.twitter.TwitterRestClient$1.doInBackground(TwitterRestClient.java:43)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
at libcore.io.Posix.getaddrinfo(Native Method)
at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:361)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
at saveourcar.text.twitter.BasicRestClient.request(BasicRestClient.java:81)
at saveourcar.text.twitter.TwitterRestClient.request(TwitterRestClient.java:17)
at saveourcar.text.twitter.BasicRestClient.postSync(BasicRestClient.java:53)
at saveourcar.text.twitter.TwitterRestClient.getBearerToken(TwitterRestClient.java:104)
at saveourcar.text.twitter.TwitterRestClient$1.doInBackground(TwitterRestClient.java:56)
at saveourcar.text.twitter.TwitterRestClient$1.doInBackground(TwitterRestClient.java:43)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
不知道爲什麼它拋出這個錯誤可以有人快速瀏覽一下吧,看看他們是否可以發現任何東西此代碼在我找到的示例中工作,但出於某種原因,它不適合我。
我的代碼休耕
我覺得這是我的問題是類 ListTweetsActivity
public class ListTweetsActivity extends AppCompatActivity {
private static final String LOG_TAG = ListTweetsActivity.class.getSimpleName();
private TwitterRestClient mHttp;
private List<Tweet> mTweets;
private ListTweetsAdapter mTweetsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tweets_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mTweets = new ArrayList<>();
mHttp = new TwitterRestClient(this);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.lst_items);
recyclerView.setLayoutManager(
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
mTweetsAdapter = new ListTweetsAdapter(this, mTweets);
recyclerView.setAdapter(mTweetsAdapter);
}
@Override
protected void onResume() {
super.onResume();
loadFeeds();
}
private void loadFeeds() {
mHttp.loadTweets("aaroadwatch", new HttpGetRequestListener() {
@Override
public void onSuccess(String result) {
Log.d(LOG_TAG, result);
List<Tweet> tweets = (new Gson()).fromJson(result,
new TypeToken<List<Tweet>>() {
}.getType());
mTweets.clear();
mTweets.addAll(tweets);
mTweetsAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Exception ex) {
Log.e(LOG_TAG, ex.getMessage());
Toast.makeText(ListTweetsActivity.this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
什麼是錯的這個問題? :1.異常信息如下:'AsyncTask#1 java.lang.RuntimeException:執行doInBackground()時發生錯誤'是useles ...使用'ex.printStackTrace()'並提供完整的logcat ... 2.太很多不必要的代碼...使用'ex.printStackTrace()'來確定問題所在的地方(在哪個類和文件中),並僅爲此部分提供代碼... – Selvin
您是否在doInBackground方法中添加了斷點以縮小它下?我不確定是否有人能夠看到你的代碼,因爲你提供的代碼太多了。 –
我只是在這個新的,我現在會更新這個問題 –