AsyncTask#1 java.lang.RuntimeException:执行doInBackground()时发生错误

时间:2016-02-05 13:13:40

标签: android android-asynctask

我只是Android开发的新手,我正在创建一个使用Twitter API来获取实时推特供稿的应用。但是,当我单击btn_twitter_stream应用程序崩溃时,我的logcat AsyncTask #1 java.lang.RuntimeException: An error occurred while executing 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 个答案:

答案 0 :(得分:2)

在您的清单中,您将未获得互联网许可

<uses-permission android:name="android.permission.INTERNET" />

清楚地显示在日志中:

  

引起:java.lang.SecurityException:权限被拒绝(缺失   互联网许可?)