我有一個intentservice將數據從手機上傳到我的服務器。每當網絡連接可用時,我都從廣播接收器啓動它。這一切工作正常,但當我通過Android 4.0.3模擬器測試它給strictmode錯誤android.os.NetworkOnMainThreadException。這就像這個網絡工作是在主線程上,但據我所知intentservice已經使工作線程。我發現這個鏈接以及http://code.google.com/p/android/issues/detail?id=23495它說移動網絡作業到後臺線程。工作線程不是後臺線程嗎?Android 4.0.3上的NetworkOnMainThreadException
編輯:這是我的廣播接收器。
public void onReceive(Context context, Intent intent) {
final String UploadingItems = "paUploadingItems";
final String preferencesFileName = "paSettings";
SharedPreferences sharedPref;
String fileQueque;
if(isNetworkAvailable(context)){
sharedPref = context.getSharedPreferences(preferencesFileName,0);
fileQueque = sharedPref.getString(UploadingItems, "");
Log.d(TAG, "quque"+fileQueque);
if(fileQueque.length() > 0){
Intent intentUpdater = new Intent(context, updaterService.class);
intentUpdater.putExtra("processMode", 6);
intentUpdater.putExtra("fileq", fileQueque.toString());
context.startService(intentUpdater);
}
}
}
private boolean isNetworkAvailable(Context context){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
Log.d(TAG, activeNetworkInfo.getTypeName());
return (activeNetworkInfo != null && activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected());
}
這是我在logcat中看到的錯誤:
04-17 15:28:58.114: E/AndroidRuntime(574): FATAL EXCEPTION: main
04-17 15:28:58.114: E/AndroidRuntime(574): java.lang.RuntimeException: Unable to start receiver org.test.dairy.OnNetworkReceiver: android.os.NetworkOnMainThreadException
04-17 15:28:58.114: E/AndroidRuntime(574): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2126)
04-17 15:28:58.114: E/AndroidRuntime(574): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
04-17 15:28:58.114: E/AndroidRuntime(574): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
04-17 15:28:58.114: E/AndroidRuntime(574): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 15:28:58.114: E/AndroidRuntime(574): at android.os.Looper.loop(Looper.java:137)
04-17 15:28:58.114: E/AndroidRuntime(574): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-17 15:28:58.114: E/AndroidRuntime(574): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 15:28:58.114: E/AndroidRuntime(574): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 15:28:58.114: E/AndroidRuntime(574): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-17 15:28:58.114: E/AndroidRuntime(574): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-17 15:28:58.114: E/AndroidRuntime(574): at dalvik.system.NativeStart.main(Native Method)
04-17 15:28:58.114: E/AndroidRuntime(574): Caused by: android.os.NetworkOnMainThreadException
04-17 15:28:58.114: E/AndroidRuntime(574): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-17 15:28:58.114: E/AndroidRuntime(574): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
04-17 15:28:58.114: E/AndroidRuntime(574): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-17 15:28:58.114: E/AndroidRuntime(574): at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-17 15:28:58.114: E/AndroidRuntime(574): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
04-17 15:28:58.114: E/AndroidRuntime(574): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-17 15:28:58.114: E/AndroidRuntime(574): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
04-17 15:28:58.114: E/AndroidRuntime(574): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
04-17 15:28:58.114: E/AndroidRuntime(574): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-17 15:28:58.114: E/AndroidRuntime(574): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
04-17 15:28:58.114: E/AndroidRuntime(574): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
04-17 15:28:58.114: E/AndroidRuntime(574): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
04-17 15:28:58.114: E/AndroidRuntime(574): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
04-17 15:28:58.114: E/AndroidRuntime(574): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
04-17 15:28:58.114: E/AndroidRuntime(574): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
04-17 15:28:58.114: E/AndroidRuntime(574): at org.test.dairy.OnNetworkReceiver.transferData(OnNetworkReceiver.java:101)
04-17 15:28:58.114: E/AndroidRuntime(574): at org.test.dairy.OnNetworkReceiver.onReceive(OnNetworkReceiver.java:57)
04-17 15:28:58.114: E/AndroidRuntime(574): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2119)
04-17 15:28:58.114: E/AndroidRuntime(574): ... 10 more
你的廣播接收器是什麼?任何網絡IO? – SeanPONeil 2012-04-17 16:44:13
是否確定網絡錯誤來自您的服務,而不是來自您的活動? – Shubhayu 2012-04-17 16:54:23
如果您希望我們評論您的實施,請發佈您的代碼。 – CommonsWare 2012-04-17 16:56:13