针对一个请求,Android Volley返回结果两次

use*_*044 8 android android-volley

我一直试图弄清楚这个问题两天了,而且我完全难以理解.出于某种原因,我正在向队列发送一个请求,但是volley返回两次,这是两次调用侦听器并在listview中将结果加倍.我打开了Volley的日志记录,我可以看到请求被添加到队列并返回,然后几秒钟后也会返回相同的请求.记录下面

V/Volley(14666): [188] CacheDispatcher.run: start new dispatcher
11-15 12:29:30.152: V/Volley(14666): [1] RequestQueue.add: Request for cacheKey=http://reallylongurl is in flight, putting on hold.
11-15 12:29:39.722: V/Volley(14666): [1] RequestQueue.finish: Releasing 1 waiting requests for cacheKey=http://reallylongurl.
11-15 12:29:39.722: D/Volley(14666): [1] MarkerLog.finish: (9809 ms) [ ] http://reallylongurl 0xd68d6603 NORMAL 1
11-15 12:29:39.732: D/Volley(14666): [1] MarkerLog.finish: (+0   ) [ 1] add-to-queue
11-15 12:29:39.732: D/Volley(14666): [1] MarkerLog.finish: (+2169) [188] cache-queue-take
11-15 12:29:39.742: D/Volley(14666): [1] MarkerLog.finish: (+37  ) [188] cache-hit
11-15 12:29:39.742: D/Volley(14666): [1] MarkerLog.finish: (+6878) [188] cache-hit-parsed
11-15 12:29:39.742: D/Volley(14666): [1] MarkerLog.finish: (+0   ) [188] post-response
11-15 12:29:39.752: D/Volley(14666): [1] MarkerLog.finish: (+725 ) [ 1] done

A few other requests get queued here.

11-15 12:29:48.405: D/Volley(14666): [1] MarkerLog.finish: (18302 ms) [ ] http://reallylongurl 0xd68d6603 NORMAL 2
11-15 12:29:48.412: D/Volley(14666): [1] MarkerLog.finish: (+0   ) [ 1] add-to-queue
11-15 12:29:48.412: D/Volley(14666): [1] MarkerLog.finish: (+15164) [188] cache-queue-take
11-15 12:29:48.412: D/Volley(14666): [1] MarkerLog.finish: (+220 ) [188] cache-hit
11-15 12:29:48.432: D/Volley(14666): [1] MarkerLog.finish: (+2299) [188] cache-hit-parsed
11-15 12:29:48.432: D/Volley(14666): [1] MarkerLog.finish: (+0   ) [188] post-response
11-15 12:29:48.442: D/Volley(14666): [1] MarkerLog.finish: (+619 ) [ 1] done
Run Code Online (Sandbox Code Playgroud)

正如你所看到的那样,它从未说过添加了另一个请求,也没有说一个请求在飞行中.如果我清除缓存,我得到相同的结果,只是第一个请求来自网络,第二个请求从缓存返回.我已经尝试过调试并单步执行我的代码,但我从未看到请求被排队多次一次.谁看过这个吗?还有什么地方我应该看看?

谢谢

编辑:以下是我开始排球的代码以及我称之为的地方.

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (savedInstanceState == null) {

            TextView emptyView = new TextView(getActivity());
            emptyView.setLayoutParams(new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            emptyView.setText("Loading....");
            emptyView.setTextSize(20);
            emptyView.setGravity(Gravity.CENTER_VERTICAL
                    | Gravity.CENTER_HORIZONTAL);

            ((ViewGroup) mListView.getParent()).addView(emptyView);
            mListView.setEmptyView(emptyView);
        }

        mAdapter = new OttoArrayAdapter(mContext);
        mListView.setOnScrollListener(onScrollListener);
        mListView.setAdapter(mAdapter);

        String url = Util.getURL("", mContext);

        HttpRequest request = new HttpRequest(url);
        request.setTag(mContext);

        VolleyLoader.getInstance(mContext).getRequestQueue().add(request);

    }



public class VolleyLoader {
    private static VolleyLoader mInstance = null;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private VolleyLoader(Context context) {

        OkHttpStack stack = new OkHttpStack();
        mRequestQueue = Volley.newRequestQueue(context, stack);
        mImageLoader = new ImageLoader(this.mRequestQueue, new LruBitmapCache(
                Util.getCacheSize(context)));
    }

    public static VolleyLoader getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new VolleyLoader(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        return this.mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        return this.mImageLoader;
    }

}
Run Code Online (Sandbox Code Playgroud)

use*_*044 6

我相信在更多地逐步完成代码后我已经弄明白了.我认为导致这种行为的是softttl.在Volley cachedispatcher中

            if (!entry.refreshNeeded()) {
                // Completely unexpired cache hit. Just deliver the response.
                mDelivery.postResponse(request, response);
            } else {
                // Soft-expired cache hit. We can deliver the cached response,
                // but we need to also send the request to the network for
                // refreshing.
                request.addMarker("cache-hit-refresh-needed");
                request.setCacheEntry(entry);

                // Mark the response as intermediate.
                response.intermediate = true;

                // Post the intermediate response back to the user and have
                // the delivery then forward the request along to the network.
                mDelivery.postResponse(request, response, new Runnable() {
                    @Override
                    public void run() {
                        try {
                            mNetworkQueue.put(request);
                        } catch (InterruptedException e) {
                            // Not much we can do about this.
                        }
                    }
                });
Run Code Online (Sandbox Code Playgroud)

这会发布响应,然后将其发送到网络,该网络会向同一个侦听器发布另一个响应.