我在使用RequestFuture排球类时遇到问题。实际上它只是停在wait(0)
; 在下面的 RequestFuture 类的 Function 内部,doGet()
并且永远不会被唤醒,onResponse
或者onErrorResponse
我认为它应该唤醒。
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();
}
这就是我尝试在上面调用所有这些的方式。
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
}
我也尝试更换线路
requestQueue.add(myReq);
和
future.setRequest(requestQueue.add(myReq));
或者
future.setRequest(myReq);
这也没有帮助。
我已经尝试了一个普通的 Volley 请求,它使用这个参数工作得很好,所以这不应该是原因。我想问题是,该请求从未真正执行过,这就是为什么永远无法到达响应侦听器的原因。也尝试过requestQueue.start()
,但没有改变任何事情。
我希望我能很好地解释我的问题,提前谢谢!