RequestFuture.get (long timeout, Timeunit), not timeout
I am having trouble using the Volley library. I don't want to wait for the request indefinitely, so I will not set a timeout. But it doesn't work. I have the same thing elsewhere (where I am using RequestFuture and not RequestFuture) and it works fine, but here I cannot get 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 be of any help it will be awesome! Thanks to
+3
source to share
1 answer
I was able to fix this problem!
The problem was that the server was responding with a 204 (NO_CONTENT) status code and was not sending content.
The most likely explanation is that since a success status code (2XX) is returned, it waits indefinitely for a JSONObject that never arrives, ignoring the timeout (because it received a success code).
0
source to share