I am using [Jobqueue] library using sync request with Volley. Everything is ok, but after a long time or performing a lot of requests after an undetermined time, I am getting this error:
Caused by: com.android.volley.VolleyError: java.lang.IllegalArgumentException: timeout < 0
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:141)
Caused by: java.lang.IllegalArgumentException: timeout < 0
at java.net.Socket.setSoTimeout(Socket.java:521)
at com.android.okhttp.internal.http.HttpTransport.discardStream(HttpTransport.java:193)
at com.android.okhttp.internal.http.HttpTransport.makeReusable(HttpTransport.java:170)
at com.android.okhttp.internal.http.HttpEngine.release(HttpEngine.java:445)
at com.android.okhttp.internal.http.AbstractHttpInputStream.endOfInput(AbstractHttpInputStream.java:86)
at com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream.read(HttpTransport.java:394)
at java.io.InputStream.read(InputStream.java:162)
at com.android.volley.toolbox.BasicNetwork.entityToBytes(BasicNetwork.java:238)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:123)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
This bug is very weird because once I get this error, requests are not working anymore. I have tried a lot of things such as having a RequestQueue per job instead of having an app instance RequestQueue, canceling all jobs and resetting jobs and requests in the RequestQueue.
This is an example of how I am use a sync request with Volley:
public class FetchBlacklistJob extends Job {
public static final String TAG = FetchBlacklistJob.class.getCanonicalName();
public FetchBlacklistJob(String groupId) {
super(new Params(Constants.JOB_PRIORITY.HIGH.getValue())
.addTags(TAG)
.setGroupId(groupId)
.singleInstanceBy(TAG)
.requireNetwork());
}
@Override
public void onAdded() {
}
@Override
public void onRun() throws Throwable {
RequestFuture<Blacklist> syncCallback = RequestFuture.newFuture();
GetBlacklistRequest request = new GetBlacklistRequest(currentBlacklist,
syncCallback, syncCallback);
syncCallback.setRequest(VolleyManager.getInstance().addRequest(request));
Blacklist response = syncCallback.get(VolleyManager.TIMEOUT, TimeUnit.MILLISECONDS);
if (response == null || response.getBlacklist() == null) {
Log.d(TAG, "response null, skipping...");
return;
}
DBUtils.saveBlacklist(response);
}
@Override
protected void onCancel(int cancelReason, @Nullable Throwable throwable) {
}
I forgot to comment that I have a timeout for each request of 15 secs and max number of retries in volley is 0.
Any idea?
Finally, I fixed this bug replacing Volley by Retrofit due to the Google library caches requests, returning always in the same thread (Main thread) and something seems to be blocked with sync requests.
A better explanation is done in this post: https://solidgeargroup.com/android-priority-job-queue-background-tasks