java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.neo.gson/com.example.neo.gson.MainActivity}: android.os.NetworkOnMainThreadException

Searched on Google with the first line of a JAVA stack trace?

We can recommend more relevant solutions and speed up debugging when you paste your entire stack trace with the exception message. Try a sample exception.

Recommended solutions based on your search

Samebug tips

,
via Stack Overflow by Razgriz

Happens when you make a network call on the main UI thread of the Android application which results in a UI freeze until the network call is complete and the response is received. Instead, execute the network calls in the background: use AsyncTask.

,

An alternative for just getting rid of the Exception is use StrictMode.ThreadPolicy to force execute on main thread.

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

Solutions on the web

via Stack Overflow by Giorgio L
, 1 year ago
Unable to start activity ComponentInfo{com.example.neo.gson/com.example.neo.gson.MainActivity}: android.os.NetworkOnMainThreadException
via Stack Overflow by J.Leo
, 4 months ago
Unable to start activity ComponentInfo{com.example.joshuac.googlevision/com.example.joshuac.googlevision.Result}: android.os.NetworkOnMainThreadException
via Stack Overflow by Hamza Khan
, 1 year ago
Unable to start activity ComponentInfo{com.hfad.viscosity/com.hfad.viscosity.ProcessData}: android.os.NetworkOnMainThreadException
via Stack Overflow by user5236815
, 1 year ago
Unable to start activity ComponentInfo{com.jbs.evecompanion/com.jbs.evecompanion.oauth_callback}: android.os.NetworkOnMainThreadException
via Stack Overflow by tccpg288
, 4 months ago
Unable to start activity ComponentInfo{com.troychuinard.fanpolls/com.troychuinard.fanpolls.ProfileActivity}: android.os.NetworkOnMainThreadException
via GitHub by rgocal
, 8 months ago
Unable to start activity ComponentInfo{com.gsd.launcher.sapphyx/com.gsd.launcher.launcher3.settings.MainSettings}: java.lang.NullPointerException: uriString
android.os.NetworkOnMainThreadException:
at java.net.InetAddress.lookupHostByName(InetAddress.java:436)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
at com.example.neo.gson.MainActivity.onCreate(MainActivity.java:47)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)

Users with the same issue

Once, 1 month ago
3 times, 6 months ago
6 times, 9 months ago
34 times, 10 months ago
4 times, 11 months ago

Write tip

Know the solutions? Share your knowledge to help other developers to debug faster.