Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Volley RequestFuture Timeout

I'm experiencing a problem when using the RequestFuture class of volley. Actually it just stops at wait(0); inside the doGet() Function in the RequestFuture class below and is never getting woken up by onResponse or onErrorResponse as I think it should.

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 the way i try to call all this above.

    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 line

requestQueue.add(myReq);

with

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

or

future.setRequest(myReq);

which didn't help either.

I already tried a usual Volley Request which worked just fine using this parameters, so that shouldn't be the cause. I guess the problem is, that the request is never actually executed, which is why the response listeners are never reached. Also tried requestQueue.start(), but didn't change a thing.

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

like image 442
user2700475 Avatar asked Sep 09 '13 14:09

user2700475


2 Answers

Adding a timeout to the get method will give the ability to catch the error, if no timeout is given then it sends the error back to the main thread. So changing JSONObject response = future.get(); with JSONObject response = future.get(REQUEST_TIMEOUT, TimeUnit.SECONDS); should do the trick and wrap it in a try catch for the timeout

like image 138
Blair Avatar answered Oct 15 '22 21:10

Blair


Solved!

JSONObject response = future.get();

Always run Future Request in seperate thread. It will not work in main thread. I am running Future Request Call in an IntentService and its working perfectly fine.

Happy Coding :)

like image 35
Farhan Avatar answered Oct 15 '22 22:10

Farhan