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... 
Cailen
source share