在使用Volley的RequestFuture
类时,我遇到了一个问题。实际上,在下面的RequestFuture
类中的doGet()
函数中的wait(0)
处停止运行,并且没有被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(),但没有任何变化。
希望我已经足够清楚地解释了我的问题, 提前感谢您!