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
  • 8,335
  • 7
  • 56
  • 84
  • 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

1 Answers1

0

You can use something like this:

/** * Check availability of web service * * @param host Address of host * @param seconds Timeout in seconds * @return Availability of host */

public static boolean checkIfURLExists(String host, int seconds)
{
    HttpURLConnection httpUrlConn;
    try
    {
        httpUrlConn = (HttpURLConnection) new URL(host).openConnection();

        // Set timeouts in milliseconds
        httpUrlConn.setConnectTimeout(seconds * 1000);
        httpUrlConn.setReadTimeout(seconds * 1000);

        // Print HTTP status code/message for your information.
        System.out.println("Response Code: " + httpUrlConn.getResponseCode());
        System.out.println("Response Message: "
                + httpUrlConn.getResponseMessage());

        return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
    catch (Exception e)
    {
        System.out.println("Error: " + e.getMessage());
        return false;
    }
}
jlopez
  • 6,287
  • 2
  • 51
  • 90