使用 AsyncTask 并仍然得到 NetworkOnMainThreadException

Using AsyncTask and still getting NetworkOnMainThreadException

好的,我一直在尝试发送 GET 请求并使用 AsyncTask class 获取结果,以便在后台完成这项工作。我新建了一个class'NetworkRequest'并继承了AsyncTaskclass,这里是代码:

package com.simpleapplication;

import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class NetworkRequest extends AsyncTask<String, Void, String> {

    public static final String REQUEST_METHOD = "GET";
    public static final int READ_TIMEOUT = 15000;
    public static final int CONNECTION_TIMEOUT = 15000;

    @Override
    protected String doInBackground(String... params){
        String stringUrl = params[0];
        String result;
        String inputLine;

        try {
            //Create a URL object holding our url
            URL myUrl = new URL(stringUrl);

            //Create a connection
            HttpURLConnection connection =(HttpURLConnection)
                    myUrl.openConnection();

            //Set methods and timeouts
            connection.setRequestMethod(REQUEST_METHOD);
            connection.setReadTimeout(READ_TIMEOUT);
            connection.setConnectTimeout(CONNECTION_TIMEOUT);

            //Connect to our url
            connection.connect();

            //Create a new InputStreamReader
            InputStreamReader streamReader = new
                    InputStreamReader(connection.getInputStream());

            //Create a new buffered reader and String Builder
            BufferedReader reader = new BufferedReader(streamReader);
            StringBuilder stringBuilder = new StringBuilder();

            //Check if the line we are reading is not null
            while((inputLine = reader.readLine()) != null){
                stringBuilder.append(inputLine);
            }

            //Close our InputStream and Buffered reader
            reader.close();
            streamReader.close();

            //Set our result equal to our stringBuilder
            result = stringBuilder.toString();
            Log.d("RESULT", result);
        }
        catch(IOException e){
            e.printStackTrace();
            result = null;
        }

        return result;
    }

    protected void onPostExecute(String result){
        super.onPostExecute(result);
    }
}

然后我在 Activity onCreate 方法中初始化了 class 并在 doInBackground 方法中传递了 URL 字符串,如下所示:

public class DashboardActivity extends AppCompatActivity {

    private String API_URL = "MY URL FOR REQUESTING DATA";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        NetworkRequest networkRequest = new NetworkRequest();
        networkRequest.doInBackground(API_URL);

    }
}

并且仍然出现 NetworkOnMainThread 异常,我不知道哪里出错了。错误日志:

01-05 12:56:56.162 12123-12123/com.smsprankster E/AndroidRuntime: FATAL EXCEPTION: main Process: com.smsprankster, PID: 12123 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smsprankster/com.smsprankster.DashboardActivity}: android.os.NetworkOnMainThreadException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2248) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.access0(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:212) at android.app.ActivityThread.main(ActivityThread.java:5151) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) at dalvik.system.NativeStart.main(Native Method) Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1154) at java.net.InetAddress.lookupHostByName(InetAddress.java:385) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) at java.net.InetAddress.getAllByName(InetAddress.java:214) at com.android.okhttp.internal.Dns.getAllByName(Dns.java:28) at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216) at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292) at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206) at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345) at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89) at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:161) at com.smsprankster.NetworkRequest.doInBackground(NetworkRequest.java:38) at com.smsprankster.DashboardActivity.onCreate(DashboardActivity.java:93) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2298)  at android.app.ActivityThread.access0(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:212)  at android.app.ActivityThread.main(ActivityThread.java:5151)  at java.lang.reflect.Method.invokeNative(Native Method)

使用这个

NetworkRequest networkRequest = new NetworkRequest();
networkRequest.execute(API_URL);

而不是这个

NetworkRequest networkRequest = new NetworkRequest();
networkRequest.doInBackground(API_URL);

参考下面的代码

NetworkRequest networkRequest = new NetworkRequest();
networkRequest.execute(API_URL);

从您的 DashboardActivity 中像这样调用

new NetworkRequest().execute(API_URL);