0

I am doing below (partly based on this answer by kuester2000) but if server is not reachable or unavailable it doesn't give the control back to the thread after 2-3 seconds. Done making request is printed after 10-15 seconds. Any idea why.

public JSONArray makeHttpRequest(String url) {

    // Making HTTP request
    try {
                    
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        // The default value is zero, that means the timeout is not used. 
        int timeoutConnection = 2000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 2000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);           
        
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
                    Log.d("error_trace", "Done making request");
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (Exception e) {
        e.printStackTrace();
        errorFlag = true;
    }
Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • 1
    it could be caused by latency in your data connection. see if it makes a difference if you're connected via wireless versus no-wireless. – droideckar Jul 27 '13 at 19:03
  • no basically i shutdown wireless and 4G and then see that control comes back after 10+ seconds to thread. – user2617341 Jul 27 '13 at 19:11

0 Answers0