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