1

I have a scenario in which i am sending requesting using Device WiFi. In this scenario WiFi is connected but WiFi further is not connected to internet and in this case i send HTTP request but the response of HTTPResonse is very slow in this case. It works fine when devices is connected to WiFi and WiFi is connected to Internet.

I need to Reduce the response time in the 1st scenario code for request is following

public String[] doHttpGetWithCode(String url, HashMap<String, String> headerParam) throws Exception {
    String[] result = new String[2];
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;
    int timeoutConnection = 3000;
    int timeoutSocket = 5000;
    HttpParams httpParameters = new BasicHttpParams();
    DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);




    httpget.addHeader("language", Constants.DEVICE_LANGUAGE);       
    Iterator myIterator = headerParam.keySet().iterator();
    while(myIterator.hasNext()) {
        String key=(String)myIterator.next();
        String value=(String)headerParam.get(key);        
        httpget.addHeader(key, value);

    }       
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    result[0] = response.getStatusLine().getStatusCode()+"";
    result[1] = sb.toString();
    return result;
}

I am using a test WiFi that has no further internet Connectivity in this case the following line of code take much time

response = httpclient.execute(httpget);

need to reduce time of response Thanks in advance

Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
  • You need to set the timeout, as is shown here: http://stackoverflow.com/questions/693997/how-to-set-httpresponse-timeout-for-android-in-java – zmbq Feb 08 '13 at 06:15

0 Answers0