0

I have an app that sends a http request to a server and receives a JSON for processing. I test it on both a physical device and Genymotion.

The app runs fine on the physical device but on Genymotion throws NetworkOnMainThreadException.

I tracked the exception and this is the part with the issue:

..
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));

// Exception on this line:
HttpResponse httpResponse = httpClient.execute(httpPost);
//
HttpEntity httpEntity = httpResponse.getEntity();
...

It seems that the Genymotion can't connect to server to execute the request. But its browser loads the sites just fine.

So, anyone knows what's going wrong here?

Mahm00d
  • 3,881
  • 8
  • 44
  • 83
  • 1
    have u called above thing inside the async task – Venkatesh S Nov 04 '13 at 12:10
  • @TonyStark I think I should. I was just reading the responses on this [question](http://stackoverflow.com/questions/9413625/android-android-os-networkonmainthreadexception?rq=1) and it seems the exception has nothing to do with Genymotion, but to the API level. My physical devices has android 2.3, thus doesn't give me this exception. – Mahm00d Nov 04 '13 at 12:16
  • I am using geny motion for me its working perfect – Venkatesh S Nov 04 '13 at 12:18
  • Run you genymotion emulator from virtual box. there you can choose the network type of the emulator. – Md. Tahmid Mozaffar Nov 04 '13 at 12:31

1 Answers1

0

I found what the problem was:

As explained in this answer, since API 11, the NetworkOnMainThreadException is thrown to inform using long-running tasks (like http communications) in the main thread.

By using AsyncTask the problem was resolved and everything worked as it should.

Community
  • 1
  • 1
Mahm00d
  • 3,881
  • 8
  • 44
  • 83