Android - volleyball RequestFuture Timeout

I'm having a problem using RequestFuture volleyball class . In fact, it just stops at wait(0) ; inside the doGet() function in the RequestFuture class below and never wakes up onResponse or onErrorResponse , as it seems to me.

 private synchronized T doGet(Long timeoutMs) throws InterruptedException, ExecutionException, TimeoutException { if (mException != null) { throw new ExecutionException(mException); } if (mResultReceived) { return mResult; } if (timeoutMs == null) { wait(0); } else if (timeoutMs > 0) { wait(timeoutMs); } if (mException != null) { throw new ExecutionException(mException); } if (!mResultReceived) { throw new TimeoutException(); } return mResult; } @Override public boolean isCancelled() { if (mRequest == null) { return false; } return mRequest.isCanceled(); } @Override public synchronized boolean isDone() { return mResultReceived || mException != null || isCancelled(); } @Override public synchronized void onResponse(T response) { mResultReceived = true; mResult = response; notifyAll(); } @Override public synchronized void onErrorResponse(VolleyError error) { mException = error; notifyAll(); } 

This is how I try to call it all up.

  RequestFuture<JSONObject> future = RequestFuture.newFuture(); JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.POST, url, jsonObj, future, future); requestQueue.add(myReq); try { JSONObject response = future.get(); } catch (InterruptedException e) { // handle the error } catch (ExecutionException e) { // handle the error } 

I also tried replacing the string

requestQueue.add(myReq);

from

future.setRequest(requestQueue.add(myReq));

or

future.setRequest(myReq);

which also did not help.

I already tried the regular Volley Request, which worked perfectly with these parameters, so this should not be the reason. I think the problem is that the request is never executed, so response listeners are never reached. Also tried requestQueue.start() , but didn't change anything.

Hope I explained my problem well enough, Thanks in advance!

+4
source share
4 answers

Adding a timeout to the get method will make it possible to catch an error if there is no timeout, then it sends the error back to the main thread. Therefore, changing JSONObject response = future.get(); using JSONObject response = future.get(REQUEST_TIMEOUT, TimeUnit.SECONDS); should do the trick and wrap it in an attempt to catch for a timeout

+5
source

Solved!

JSONObject response = future.get ();

Always run a future request in a separate thread. It will not work in the main topic. I run Future Request Call in IntentService and it works fine.

Happy coding :)

+3
source

You need to call future.get() in another thread. Try wrapping it in AsyncTask.

Looking at the source, it seems that RequestQueue launches a long chain of calls to load the response and which eventually ends with RequestFuture.onResponse (which, in turn, calls notifyAll to notify the thread about the termination of the wait, as you mentioned). I think the problem is that the wait(0) chain and RequestQueue run in the user interface thread, so when wait(0) is called, the RequestQueue chain also waits, so onResponse never called, so the line just hangs (because wait ( 0) waiting forever).

0
source

In addition to the decisions of Blair and Farhan, adding a retry policy to the query also does the job.

 request.setRetryPolicy(new DefaultRetryPolicy(RETRY_TIME, RETRY_ATTEMPTS, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
0
source

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


All Articles