I'm having trouble using the Volley library. I don't want to wait for the request indefinitely, so I wan't to set a timeout. But it isn't working. I have the same thing in other places (where I use a RequestFuture and not a RequestFuture) and it's working fine, but in here I can't set it to work.
static final long TIMEOUT = 3;
static final TimeUnit TIMEOUT_TIME_UNIT = TimeUnit.SECONDS;
public boolean fetchPoiUserRating(PoiModel poi, String userId) throws RemoteDataException {
RequestQueue queue = mRequestQueue;
String url = API_POI_RATING_URL + "/" + poi.getId() + "/" + userId;
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, future, future);
future.setRequest(queue.add(request));
try {
Log.d(TAG, "Start poi user rating fetch");
JSONObject response = future.get(TIMEOUT, TIMEOUT_TIME_UNIT); // this should timeout but it doesn't
Log.d(TAG, "Finished poi user rating fetch");
poi.setUserRating((float) response.getDouble("rating"));
return true;
} catch (InterruptedException | ExecutionException | JSONException | TimeoutException e) {
handleError(e);
return false;
}
}
If you can provide any help it would be amazing! Thanks
I was able to fix the issue!
The problem was that the server was answering with status code 204 (NO_CONTENT) and it wasn't sending any content.
The most likely explanation is that because a success status code is being returned (2XX), it waits indefinitely for a JSONObject that never comes, ignoring the timeout (because it received a success code).