-2

我正在尝试检查我的移动设备是否可以访问互联网,但出现以下错误:

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

在我的应用程序中,我需要通过 WIFI 或数据连接检查设备是否有互联网连接。所以我测试是否有与谷歌的连接。如果这种方法可能是我的问题的原因,则不是。这是我正在使用的代码。

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);
    }

}

任何的想法?谢谢!!

4

2 回答 2

4

NetworkOnMainThreadException

当应用程序尝试在其主线程上执行网络操作时引发的异常。

所以你需要使用ThreadrunOnUiThreadAsyncTaskHandlerHandlerThread从后台线程更新 UI 元素。

于 2012-08-13T12:43:39.370 回答
1

您正在尝试在主线程上执行网络操作,在 Android 应用程序的主 UI 线程中是不允许的。

看看NetworkOnMainThreadException

要解决此问题,请使用AsyncTaskThread 并将您的网络相关操作放入其中。

于 2012-08-13T12:44:13.540 回答