-1

Im trying to use async to make an api call, sometimes the request takes longer than 10 secs, is there a way to make the request wait for 30 secs

new Act().execute( val, val2, "" );

 public class Act extends AsyncTask<String, String, String> {

        protected String doInBackground( String... params ) {
            return "";
        }

        @Override
        protected void onPostExecute( String pResult ) {
           
        }
    }
Pedro
  • 1,440
  • 2
  • 15
  • 36
  • I don't think this has to do with the AsyncTask itself, it's most likely the library/method timeout that you are using to make the request. – javdromero Jul 12 '21 at 16:47
  • @javdromero the request only waits for 10 secs everytime – Pedro Jul 12 '21 at 17:06
  • As @javdromero said it's related to the HTTP client you use. If you want to set request read/write timeout you can do this in the HTTP client initialization. Please edit the question and add the name of the library you use to make it easy to help you. Also `AsyncTask` is deprecated now, you can use something like `RxJava` or your HTTP client-provided callbacks. – Mohamed Jul 12 '21 at 17:09

1 Answers1

0

Retrofit & OkHttp are a more targeted approach to this problem. You can have a client created via OkHttp with a timeout assigned to it. Here is an example How to set timeout in Retrofit library? In that same example you can find a basic Retrofit example.

AsyncTask just keeps things off the UIThread that's it. Its life time is decided by the contents of your implementation => Act, not the AsyncTask its self.

avalerio
  • 2,072
  • 1
  • 12
  • 11