ChuckKelly
ChuckKelly

Reputation: 1740

Set Timeout on Asynchronous Http Client

im using the Asynchronous Http Client that can be found here: http://loopj.com/android-async-http/

and it works great besides about 1 out of every 10 or so requests I make end up giving me a infinite progress dialog which I believe means for whatever reason no response of any kind is being returned because I have written code to dismiss the dialog in onSuccess AND onFailure so im a bit confused how this could happen.

Here is my code that sets up the request:

  public static void post(String token,String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
     Log.i(token,"token");
      client.addHeader("token", token);
      client.setTimeout(3000);
      client.post(url, params, responseHandler);


  }

And here is where i override onSuccess and onFailure:

@Override
            public void onFailure(Throwable arg0, String arg1) {
                // TODO Auto-generated method stub
                super.onFailure(arg0, arg1);
                pdialog.dismiss();
                Log.i("failed to login", arg1.toString());
                Toast.makeText(getActivity(), arg1.toString() , Toast.LENGTH_LONG).show();
            }

            @Override
            public void onSuccess(final JSONObject json) {
                pdialog.dismiss();
    }

Upvotes: 4

Views: 12587

Answers (2)

ChuckKelly
ChuckKelly

Reputation: 1740

After much frustration I gave up on figuring this out BUT the latest version DOES make it very easy to accomplish this if you just upgrade your lib.

Upvotes: 0

Rajeev
Rajeev

Reputation: 1404

The library seems to be doing what you want it to do, setTimeout code from the AsyncHttpClient class

public void setTimeout(int timeout){
    final HttpParams httpParams = this.httpClient.getParams();
    ConnManagerParams.setTimeout(httpParams, timeout);
    HttpConnectionParams.setSoTimeout(httpParams, timeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
}

If it is not working then better report the issue here

Upvotes: 2

Related Questions