NetworkOnMainThreadException While I see if I have

2019-09-21 13:26发布

I am trying to check if my mobile device has internet access but I get the following error:

08-13 14:20:48.990: E/AndroidRuntime(2546): FATAL EXCEPTION: main
08-13 14:20:48.990: E/AndroidRuntime(2546): java.lang.RuntimeException: Unable to start activity ComponentInfo{cx.hell.android/cx.hell.android.conn.tryInternet}: android.os.NetworkOnMainThreadException
08-13 14:20:48.990: E/AndroidRuntime(2546):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at android.os.Looper.loop(Looper.java:137)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at java.lang.reflect.Method.invoke(Method.java:511)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at dalvik.system.NativeStart.main(Native Method)
08-13 14:20:48.990: E/AndroidRuntime(2546): Caused by: android.os.NetworkOnMainThreadException
08-13 14:20:48.990: E/AndroidRuntime(2546):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at cx.hell.android.conn.tryInternet.executeReq(tryInternet.java:47)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at cx.hell.android.conn.tryInternet.onCreate(tryInternet.java:27)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at android.app.Activity.performCreate(Activity.java:5008)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-13 14:20:48.990: E/AndroidRuntime(2546):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-13 14:20:48.990: E/AndroidRuntime(2546):     ... 11 more

In my application, I need to check if the device has internet connection, either by WIFI or data connection. So I test if there is connectivity with google. Not be if this method can be the cause of my problem. This is the code I'm using.

public class tryInternet extends Activity{
    String LOGTAG="";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            //LOAD LIBRARY//
            URL url;
            try {
                url = new URL("http://www.google.es");
                executeReq(url);
                Intent intent = new Intent(tryInternet.this, MainActivity.class);
                startActivity(intent);
                finish();
            } catch (IOException e) {
                Toast.makeText(getBaseContext(), "DON'T HAVE A CONNECTION OF INTERNET", 
                        Toast.LENGTH_LONG).show();
            }

    }
    private void executeReq(URL urlObject) throws IOException{
        HttpURLConnection conn = null;

        conn = (HttpURLConnection) urlObject.openConnection();
        conn.setReadTimeout(100000);//milliseconds
        conn.setConnectTimeout(150000);//milliseconds
        conn.setRequestMethod("GET");
        conn.setDoInput(true);

        // Start connect
        conn.connect();
        InputStream response = conn.getInputStream();
        String iS= "" + response;
        Log.d("Response:", iS);
    }

}

any idea? Thankss!!

2条回答
可以哭但决不认输i
2楼-- · 2019-09-21 13:44

NetworkOnMainThreadException:

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

so you need to Use Thread, runOnUiThread, AsyncTask , Handler , or HandlerThread for Updating UI elements from background Thread.

查看更多
Emotional °昔
3楼-- · 2019-09-21 13:53

You are trying to perform a networking operation on main thread, it's not allowed in Main UI Thread of Android Application.

Look at NetworkOnMainThreadException

To solve this either use AsyncTask or Thread and put your Network Related Operation in it.

查看更多
登录 后发表回答