5

ボレーのRequestFutureクラスを使用しているときに問題が発生しています。wait(0)実際には;で止まります。以下のRequestFutureクラスの関数内で、または私がそうすべきだと思うようにdoGet()目覚めることはありません。onResponseonErrorResponse

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 Request を既に試したので、それが原因ではないはずです。問題は、リクエストが実際に実行されないことだと思います。これが、レスポンスリスナーに到達しない理由です。も試しrequestQueue.start()ましたが、何も変わりませんでした。

問題を十分に説明できれば幸いです。よろしくお願いします。

4

4 に答える 4

3

解決しました!

JSONObject レスポンス = future.get();

Future Request は常に別のスレッドで実行します。メインスレッドでは機能しません。私は IntentService で Future Request Call を実行していますが、完全に正常に動作しています。

ハッピーコーディング:)

于 2015-02-05T09:12:21.833 に答える