Thursday, June 20, 2013

Android: Solution for NetworkOnMainThreadException


NetworkOnMainThreadException
Occurs in Runtime Exception
The exception that is thrown when an application attempts to perform a networking operation on its main thread. (NetworkOnMainThreadException)

android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork
StrictMode : StrictMode is most commonly used to catch accidental disk or network access on the application's main thread. (StrictMode.ThreadPolicy.Builder)
From above statement, we can conclude there is some network operation is performed in Main Therad.
Exception:
android.os.NetworkOnMainThreadException
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
libcore.io.IoBridge.connectErrno(IoBridge.java:127)
libcore.io.IoBridge.connect(IoBridge.java:112)
java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
java.net.Socket.connect(Socket.java:842)
org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
.
.
.
android.view.View.performClick(View.java:4204)
android.view.View$PerformClick.run(View.java:17355)
android.os.Handler.handleCallback(Handler.java:725)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:5041)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
dalvik.system.NativeStart.main(Native Method)

Solutions:
Remove Network operation from main thread and use background operations like Services, Async Task
Or
StrictMode implemented from api level 9, so your min android sdk support should be 9

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

7 comments :

  1. in AndroidManifest.xml
    you add a code:
    uses-permission android:name="android.permission.INTERNET"
    uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"

    ReplyDelete
    Replies
    1. You are right, When we try network related operation, we need to implement the above code in manifest file but the error is not about missing of the syntax of the xml.
      When we run network operation on main thread then we face the following exception to overcome the issue, solution is provided.

      Delete
    2. Because this site del sige open and close tag xml => you add tag open anh close for each line in the.

      Delete
    3. Sorry, I am not able to get your question?

      Delete
  2. Am Doing TWitter api app i got exception called ii did stract mode method in my on create methos is not working please help on this 04-08 00:28:53.076: E/AndroidRuntime(1836): FATAL EXCEPTION: main
    04-08 00:28:53.076: E/AndroidRuntime(1836): Process: com.androidhive.twitterconnect, PID: 1836
    04-08 00:28:53.076: E/AndroidRuntime(1836): android.os.NetworkOnMainThreadException
    04-08 00:28:53.076: E/AndroidRuntime(1836): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at java.net.InetAddress.getAllByName(InetAddress.java:214)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:158)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:121)
    04-08 00:28:53.076: E/AndroidRuntime(1836): at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)

    ReplyDelete
    Replies
    1. By the exception, we can identify your network call is happening on main thread.
      Try to run the network code in Async Task or in different thread.

      Delete
  3. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    ReplyDelete