Home  >  Forum  >  Android
Post New Query

E/AndroidRuntime : Caused by: android.os.NetworkOnMainThreadException


joined on
November 02,2014
Asked on November 02,2014
Hello Friends,

I am creating an android application. I am integrating android and spring but got the issue. 

11-01 16:56:07.100: E/AndroidRuntime(999): Caused by: android.os.NetworkOnMainThreadException
11-01 16:56:07.100: E/AndroidRuntime(999): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
11-01 16:56:07.100: E/AndroidRuntime(999): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-01 16:56:07.100: E/AndroidRuntime(999): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-01 16:56:07.100: E/AndroidRuntime(999): at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-01 16:56:07.100: E/AndroidRuntime(999): at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
11-01 16:56:07.100: E/AndroidRuntime(999): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
11-01 16:56:07.100: E/AndroidRuntime(999): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
11-01 16:56:07.100: E/AndroidRuntime(999): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
11-01 16:56:07.100: E/AndroidRuntime(999): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
11-01 16:56:07.100: E/AndroidRuntime(999): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
11-01 16:56:07.100: E/AndroidRuntime(999): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:351)
11-01 16:56:07.100: E/AndroidRuntime(999): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:90)
11-01 16:56:07.100: E/AndroidRuntime(999): at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:72)
11-01 16:56:07.100: E/AndroidRuntime(999): at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46)
11-01 16:56:07.100: E/AndroidRuntime(999): at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:63)
11-01 16:56:07.100: E/AndroidRuntime(999): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:476)
11-01 16:56:07.100: E/AndroidRuntime(999): at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
11-01 16:56:07.100: E/AndroidRuntime(999): at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)

joined on
October 10,2013
Replied on November 02,2014

NetworkOnMainThreadException  is thrown by android in the case when we are fetching data from a network within main thread of activity class. This is because android avoids UI blocking. To perform network  task
 we can do as below.
1. Create a background thread using android AsyncTask.
2. Before calling connection use below code

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

joined on
October 10,2013
Replied on November 02,2014

Write Answer



Sitewide-Feb4of4234x60





Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us