Android的凌空返回结果两次,每次一个请求 [英] Android Volley Returning results twice for one request

查看:197
本文介绍了Android的凌空返回结果两次,每次一个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力,现在要弄清楚这个问题了两天,我完全地难住了。出于某种原因,我发送一个请求队列,但凌空抽射被退回两次,这是呼吁听众两次,结果在我的列表视图增加​​了一倍。我打开日志记录排球,我可以看到该请求被添加到队列中并返回,然后几秒钟后,相同的请求也被退回。下面登录

  V /排球(14666):[188] CacheDispatcher.run:启动新的调度
12月11号至15日:29:30.152:V /排球(14666):[1] RequestQueue.add:请给cacheKey = HTTP:// reallylongurl是在飞行中,将搁置。
12月11号至15日:29:39.722:V /排球(14666):[1] RequestQueue.finish:解除问题cacheKey = HTTP 1等待请求:// reallylongurl。
12月11号至15日:29:39.722:D /排球(14666):[1] MarkerLog.finish:(9809毫秒)[] HTTP:// reallylongurl 0xd68d6603师范大学1
12月11号至15日:29:39.732:D /排球(14666):[1] MarkerLog.finish:(+0)[1]加入到队列
12月11号至15日:29:39.732:D /排球(14666):[1] MarkerLog.finish:(2169)[188]缓存队列走
12月11号至15日:29:39.742:D /排球(14666):[1] MarkerLog.finish:(+37)[188]缓存命中
12月11号至15日:29:39.742:D /排球(14666):[1] MarkerLog.finish:(6878)[188]缓存命中解析
12月11号至15日:29:39.742:D /排球(14666):[1] MarkerLog.finish:(+0)[188]后的反应
12月11号至15日:29:39.752:D /排球(14666):[1] MarkerLog.finish:(725)[1]就可以完成

其他一些请求到这里来排队。

12月11号至15日:29:48.405:D /排球(14666):[1] MarkerLog.finish:(18302毫秒)[] HTTP:// reallylongurl 0xd68d6603师范大学2
12月11号至15日:29:48.412:D /排球(14666):[1] MarkerLog.finish:(+0)[1]加入到队列
12月11号至15日:29:48.412:D /排球(14666):[1] MarkerLog.finish:(15164)[188]缓存队列走
12月11号至15日:29:48.412:D /排球(14666):[1] MarkerLog.finish:(+220)[188]缓存命中
12月11号至15日:29:48.432:D /排球(14666):[1] MarkerLog.finish:(2299)[188]缓存命中解析
12月11号至15日:29:48.432:D /排球(14666):[1] MarkerLog.finish:(+0)[188]后的反应
12月11号至15日:29:48.442:D /排球(14666):[1] MarkerLog.finish:(619)[1]就可以完成
 

正如你可以看到它永远不会说,另一个请求加入,也不多说了那么一个要求是在飞行中。如果我清除高速缓存我得到了相同的结果,只是第一个请求是从网络,二是从缓存中返回。我曾尝试调试和步进通过我的code,但我从来没有看到该请求被加入队列超过一次。有没有人见过这个?任何其他的地方我应该看?

感谢

编辑:这是code,我开始了排球和在那里我称之为

  @覆盖
    公共无效onActivityCreated(包savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        如果(savedInstanceState == NULL){

            TextView的emptyView =新的TextView(getActivity());
            emptyView.setLayoutParams(新的LayoutParams(
                    LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
            emptyView.setText(加载......);
            emptyView.setTextSize(20);
            emptyView.setGravity(Gravity.CENTER_VERTICAL
                    | Gravity.CENTER_HORIZONTAL);

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

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

        字符串URL = Util.getURL(,mContext);

        HTT prequest请求=新HTT prequest(URL);
        request.setTag(mContext);

        。VolleyLoader.getInstance(mContext).getRequestQueue()加(要求);

    }



公共类VolleyLoader {
    私有静态VolleyLoader mInstance = NULL;
    私人请求队列mRequestQueue;
    私人ImageLoader的mImageLoader;

    私人VolleyLoader(上下文的背景下){

        OkHttpStack堆栈=新OkHttpStack();
        mRequestQueue = Volley.newRequestQueue(背景下,栈);
        mImageLoader =新ImageLoader的(this.mRequestQueue,新LruBitmapCache(
                Util.getCacheSize(上下文)));
    }

    公共静态VolleyLoader的getInstance(上下文的背景下){
        如果(mInstance == NULL){
            mInstance =新VolleyLoader(上下文);
        }
        返回mInstance;
    }

    公共请求队列getRequestQueue(){
        返回this.mRequestQueue;
    }

    公共ImageLoader的getImageLoader(){
        返回this.mImageLoader;
    }

}
 

解决方案

我相信我已经想通了通过code步进更后。我相信这是造成这种现象的softttl。在排球cachedispatcher

 如果(!entry.refreshNeeded()){
                //完全过期的缓存命中。刚刚传送的响应。
                mDelivery.postResponse(请求,响应);
            } 其他 {
                //软过期的缓存命中。我们可以提供缓存的响应,
                //但是我们还需要将请求发送到网络
                //耳目一新。
                request.addMarker(缓存命中刷新需要的);
                request.setCacheEntry(入口);

                //标记的响应作为中间。
                response.intermediate = TRUE;

                //邮政中间响应返回给用户,并有
                //交付则请求跟着网络转发。
                mDelivery.postResponse(请求,响应,新的可运行(){
                    @覆盖
                    公共无效的run(){
                        尝试 {
                            mNetworkQueue.put(要求);
                        }赶上(InterruptedException异常E){
                            //不多,我们可以做到这一点。
                        }
                    }
                });
 

这个帖子的响应然后将其发送到网络中哪些职位另一个响应相同的监听器。

I have been trying to figure out this issue for two days now and I am completly stumped. For some reason I am sending one request to the queue but volley is returning it twice, which is calling the listener twice and doubling the results in my listview. I turned on the logging for Volley and I can see the request being added to the queue and returned, then a few seconds later the same request is also returned. Log below

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

As you can see it never says another request was added nor does it say more then one request was in flight. If I clear out the cache I get the same result, just the first request is from network and the second is returned from cache. I have tried debugging and stepping through my code but I never see the request being enqueued more then once. Has anyone seen this before? Any where else I should look?

Thanks

Edit: Here is the code where I start up volley and where I call it.

@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;
    }

}

解决方案

I believe I have figured it out after stepping through the code more. I believe it is the softttl that is causing this behavior. In the 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.
                        }
                    }
                });

This posts the response then sends it to the network which posts another response to the same listener.

这篇关于Android的凌空返回结果两次,每次一个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆