Setting a timeout for the Http asynchronous client

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

and it works fine, except for about 1 out of every 10 or so requests that I end up giving me an endless dialogue on progress, which I think means for some reason, no kind answer is coming back, because I wrote code to reject the dialogue in onSuccess AND onFailure, so I was a little confused about 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 this is where I redefine 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(); } 
+4
source share
3 answers

The library seems to be doing what you want 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 does not work, better report a problem here

+1
source

After much disappointment, I gave up on this, but the latest version makes it very easy if you just upgrade your library.

0
source

I think the best way is to use the following overridden methods

  @Override public void onStart() { super.onStart(); pdialog.dismiss(); } @Override public void onFinish() { super.onFinish(); pdialog.dismiss(); } 
0
source

Source: https://habr.com/ru/post/1492001/


All Articles