Marjan
Marjan

Reputation: 123

NetworkOnMainThreadException on Android 4.0.3

I have an intentservice which uploads data from phone to my server. Whenever network connection is available I start it from a broadcast receiver. It all works fine but when I test it by Android 4.0.3 emulator it gives strictmode error android.os.NetworkOnMainThreadException. It's like this network job is on main thread but as far as I know intentservice has makes a worker thread. I found this link as well http://code.google.com/p/android/issues/detail?id=23495 which says move network job to background thread. Isn't worker thread a background thread?

Edited: this is my broadcast receiver.

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

}

And this is the error I see in 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

Upvotes: 1

Views: 4263

Answers (2)

Jeremy Edwards
Jeremy Edwards

Reputation: 14740

Move the code into the IntentService. The Broadcast receiver's onReceive method runs on the UI thread and the network code is causing it to crash. Simply redirect the intent to the IntentService and you should be fine.

This makes the code simpler as well. Receivers aren't really meant to do complex logic they are there to redirect messages from the system to your app. Services are meant to do heavy work in the background.

Upvotes: 0

SeanPONeil
SeanPONeil

Reputation: 3910

Your isNetworkAvailable() method checks for network I/O on the main UI thread, and Android considers that blocking. I would just fire off the request to the IntentService without checking for connectivity in the Activity, and let the IntentService throw an exception and report back to the Activity if it can't make a connection.

Upvotes: 2

Related Questions