Android HttpURLConnection is VERY Slow

This is the situation I came across with the code below:

As you can see, I am trying to read an HTTP stream. When I run the following code on an Android simulator, it works 100% of the time, when I run the following code on my Galaxy S3, and in 3G it works 100% of the time when I try to connect to the URL using my laptop, the browser works 100% of the time when I try to connect using the Galaxy S3 browser (both in wifi and in 3g), it works ... in 100% of cases. HOWEVER, when I try to connect using my Galaxy S3, and on Wi-Fi I timeout ~ 80% of the time. If I remove the timeout properties, I get strange exceptions, such as:

"recvfrom failed: ETIMEDOUT" "failed to connect to <URL>: connect failed: ENETUNREACH (Network is unreachable)" "unable to resolve the host <URL>: no address associated with hostname" 

I am open to any suggestions ...

 public static final String getHttpResponse(String url) { HttpURLConnection conn = null; InputStream response = null; try { URL address = new URL(url); conn = (HttpURLConnection)address.openConnection(); conn.setConnectTimeout(30 * 1000); //30 seconds conn.setReadTimeout(30 * 1000); //30 seconds response = conn.getInputStream(); if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) { Log.e("Util.getHttpResponse", Integer.toString(conn.getResponseCode())); return null; } String result = Util.streamToString(response); return result; } catch(IOException e) { response = conn.getErrorStream(); Log.e("Util.getHttpResponse", Util.streamToString(response)); return null; } finally { if( response != null ) { try { response.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn != null) { conn.disconnect(); } } } 

UPDATE: - using AndroidHttpClient did not help - After receiving the input stream, I got an error pop-up in the Eclipse IDE ... As you can see, my debug cursor did everything until line 107 .. well after I finished receiving the input stream on this time... Eclipse

+1
android timeout galaxy
source share
2 answers

I have the same problem on an Android device. I am using the IP address in the url. Final, I found that the HttpURLConnection.connect (...) method included getHostName () internally. After that, I use the domain in the URL, then it works in 100% of cases.

+1
source

How to experiment, what if you try to use the AndroidHttpClient class instead to do the same? It has some predefined timeouts and other settings, which, we were told, should work fine in most cases.

0
source

All Articles