1

I am using Loopj's AsyncHttpclient in my android App to send asynchronous requests to fetch data. I want to cancel the task if there is no response from the server or if it takes more than 10seconds to connect to the server. I tried setting timeout, it cancels the task after the specified duration whereas if I do not set any timeout, I get response as expected. Here is the

public class HttpRestClient {

private final static String BASE_URL = "http://test.com/service.svc/";

private static AsyncHttpClient client = new AsyncHttpClient();

  public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      client.setTimeout(2000);
      client.get(getAbsoluteUrl(url), params, responseHandler);
  }

  public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      client.setTimeout(2000);
      client.post(getAbsoluteUrl(url), params, responseHandler);
  }

  public static void postJSON(Context context, String url, HttpEntity entity, String contentType, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      client.setTimeout(2000);
      client.post(context, getAbsoluteUrl(url), entity, contentType, responseHandler);
  }

  private static String getAbsoluteUrl(String relativeUrl) {
      return BASE_URL + relativeUrl;
  }

}
Monish Kamble
  • 1,478
  • 1
  • 15
  • 28

0 Answers0