HTTP does not work in Android emulator

I tried several HTTP classes ( HttpURLConnection , HTTPClient and others), but they do not work in the emulator. Then I decided to check it out on my phone and it worked fine!

So, how can I fix this strange behavior of the Android emulator that the HTTP classes do not work (while the browser can work)? They generally crash the application.

Here is my code:

 public static SimpleXML getResponse(String action, Map<String, String> params) { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(action); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size()); for(Map.Entry<String, String> heh : params.entrySet()) nameValuePairs.add(new BasicNameValuePair(heh.getKey(), heh.getValue())); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); return SimpleXML.loadXml(response.getEntity().getContent()); } catch (ClientProtocolException e) { return null; } catch (IOException e) { return null; } } 

LogCat Magazine:

 06-30 22:07:28.972: E/AndroidRuntime(682): FATAL EXCEPTION: main 06-30 22:07:28.972: E/AndroidRuntime(682): android.os.NetworkOnMainThreadException 06-30 22:07:28.972: E/AndroidRuntime(682): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 06-30 22:07:28.972: E/AndroidRuntime(682): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 06-30 22:07:28.972: E/AndroidRuntime(682): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 06-30 22:07:28.972: E/AndroidRuntime(682): at java.net.InetAddress.getAllByName(InetAddress.java:214) 06-30 22:07:28.972: E/AndroidRuntime(682): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 06-30 22:07:28.972: E/AndroidRuntime(682): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 06-30 22:07:28.972: E/AndroidRuntime(682): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 06-30 22:07:28.972: E/AndroidRuntime(682): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 06-30 22:07:28.972: E/AndroidRuntime(682): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 06-30 22:07:28.972: E/AndroidRuntime(682): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 06-30 22:07:28.972: E/AndroidRuntime(682): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 06-30 22:07:28.972: E/AndroidRuntime(682): at net.ekvium.air.API.getResponse(API.java:98) 06-30 22:07:28.972: E/AndroidRuntime(682): at net.ekvium.air.MainActivity$1.onClick(MainActivity.java:62) 06-30 22:07:28.972: E/AndroidRuntime(682): at android.view.View.performClick(View.java:4084) 06-30 22:07:28.972: E/AndroidRuntime(682): at android.view.View$PerformClick.run(View.java:16966) 06-30 22:07:28.972: E/AndroidRuntime(682): at android.os.Handler.handleCallback(Handler.java:615) 06-30 22:07:28.972: E/AndroidRuntime(682): at android.os.Handler.dispatchMessage(Handler.java:92) 06-30 22:07:28.972: E/AndroidRuntime(682): at android.os.Looper.loop(Looper.java:137) 06-30 22:07:28.972: E/AndroidRuntime(682): at android.app.ActivityThread.main(ActivityThread.java:4745) 06-30 22:07:28.972: E/AndroidRuntime(682): at java.lang.reflect.Method.invokeNative(Native Method) 06-30 22:07:28.972: E/AndroidRuntime(682): at java.lang.reflect.Method.invoke(Method.java:511) 06-30 22:07:28.972: E/AndroidRuntime(682): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 06-30 22:07:28.972: E/AndroidRuntime(682): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 06-30 22:07:28.972: E/AndroidRuntime(682): at dalvik.system.NativeStart.main(Native Method) 
+11
android
Jun 30 '12 at 21:16
source share
3 answers

If you look at this Android documentation , it explains

NetworkOnMainThreadException:

An exception that occurs when an application attempts to perform network operations in the main thread.

This is only for applications targeting the Honeycomb SDK or higher. Allowed to use applications that focus on earlier versions of the SDK. Networks in their main event loop threads, but this is highly discouraged.

Thus, depending on the OS version, there may be a forced (exception) policy that you do not make network requests in the user interface thread. This may explain why your code runs on the device, and not on the emulator (if they have different versions of Android).

You can change ThreadPolicy . But as an alternative, I would suggest you look again at the expression in Android docs. They greatly interfere with network operations in the main thread, and I certainly agree with them.

So, instead of changing the policy to make it legal, you might consider changing your code so that your getResponse() method is not called in the user interface thread.

Generally, you should use AsyncTask to run in the background .

+20
Jun 30 '12 at 22:28
source

This happens because you are trying to do network activity in the main thread.

I had the same problem, it worked for a while, and then a few weeks after its development, it stopped working.

The solution I found is to add these lines to

 onCreate() 

Method:

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

Hope this works for you too.

EDIT

Due to the increase in the number of upvotes, I want to add something. This will remove NetworkingOnMainThreadException , HOWEVER, this is far from the recommended method.

This exception exists for some reason. Network activity can take a lot of time, and network work on the main thread, which is the same thread that is responsible for updating the user interface, will freeze the thread until the network is completed (this is what happens in each thread, but when it runs on the selected thread, this is normal). On Android, if the UI thread is inactive for 5 seconds, it will display the Application is not responsive, Do you want to close it? dialog Application is not responsive, Do you want to close it? .

This is what the exception came about to prevent. Setting a policy, as I said, eliminates the exception, is the wrong way to perform actions. Any network actions must be performed in a separate thread, either using AsyncTask , or by manually creating a new Thread . AsyncTask is a very simple and easy way to implement this, and this is what I recommend.

Please consider this change when using my answer.

Greetings

+15
Jun 30 '12 at 22:14
source

You disable strict mode using the following code:

 if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } 

This is not recommended: use the AsyncTask interface.

AsyncTask Link

Link of another link

0
Sep 11 '15 at 5:57
source



All Articles