Volley - 让请求等待上一个请求完成

Volley - Make request wait for previous request to complete

我正在尝试从 json 获取 url 图像,然后使用该 url 下载图像。但问题是 url 被获取,所以 late/slow imagerequest 报告 urlFull 为空。即使在使用 DataCallback 接口之后。有什么解决方案可以让两个请求同步发生吗?

编辑- 我注意到一件奇怪的事情是,当我第二次单击按钮时,这段代码运行良好。但是第一次它说 Bad url null

我已经在调用的按钮上设置了 onclicklistener-

public void fetchSave(String photoJson) {
    fetchData(new DataCallback() {
        @Override
        public void onBitmapSuccess(Bitmap result) {
            Utils utils = new Utils(getActivity());
            utils.saveImageSDCard(result);
        }

        @Override
        public void onJsonSuccess(JSONObject result) {
            try {
                JSONArray mediaContentArray = result.getJSONObject(TAG_ENTRY).getJSONObject(TAG_MEDIA_GROUP).getJSONArray(TAG_MEDIA_CONTENT);
                JSONObject mediaObject = (JSONObject) mediaContentArray.get(0);
                urlFull = mediaObject.getString(TAG_IMG_URL);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.d(TAG, "Full image url " + urlFull);
        }
    }, photoJson);
}

fetchData()如下-

public void fetchData(final DataCallback callback, String photoJson) {
    RequestFuture<JSONObject> future = newFuture();
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, photoJson, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, "JsonObject response= "+response.toString());
            callback.onJsonSuccess(response);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), getString(R.string.toast_unknown_error), Toast.LENGTH_LONG).show();
        }
    });

    jsonObjectRequest.setShouldCache(false);
    AppController.getInstance().addToRequestQueue(jsonObjectRequest);
    AppController.getInstance().getRequestQueue().getCache().remove(photoJson);


        final ImageRequest imageRequest = new ImageRequest(urlFull, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                Log.d(TAG, "Bitmap response");
                callback.onBitmapSuccess(response);
            }
        }, 0, 0, ImageView.ScaleType.CENTER_CROP, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), "Couldn't Download", Toast.LENGTH_SHORT).show();
                error.printStackTrace();

            }
        });
        AppController.getInstance().addToRequestQueue(imageRequest);
}

Volley 支持通过 RequestFutures 阻塞请求。您创建一个普通请求,但将其回调设置为您的请求未来,这只是 volley 对标准 java 未来的扩展。对 future.get() 的调用将阻塞。

看起来像这样

RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(Method.POST, SIGNUP_URL, reqBody, future, future)
volleyRequestQueue.add(request);

try {
    JSONObject response = future.get();
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}

您可以在第一个请求的 onResponse() 方法中发送第二个请求,这样您就可以确保数据不为空