我想檢查我的移動設備,寬帶上網,但我得到了以下錯誤:NetworkOnMainThreadException雖然我看看我是否有互聯網連接
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);
}
}
有什麼想法嗎? Thankss!
在搜索上省力......以任何方式將連接檢查代碼放入'AsyncTask'中。並刪除這個問題..因此有很多問題已經可用於相同的主題。 – user370305 2012-08-13 12:43:43
我已經看到很多帖子出現這個問題,但是idk如果真的需要添加一個asynctask來解決這個問題 – jlopez 2012-08-13 12:46:44
'AsyncTask'沒有必要。但是您的網絡相關操作不應該在'MainUI線程'上。所以你可以使用線程,處理程序等。但主要規則是**不在MainUI線程**上。 – user370305 2012-08-13 12:48:17