如何检查Volley Request Queue是否为空?Request是否完成? [英] How to check Volley Request Queue is empty?And Request is finished?

查看:82
本文介绍了如何检查Volley Request Queue是否为空?Request是否完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查排球请求队列是否为空?请求已完成?

完成所有请求后,我尝试加载ui,但它在请求响应之前加载

After finishing of all requests I am trying to load ui but it's loading before requests response

for (i = 1; i < obj.length; i++) {

            String apiString = myGlobalClass.getApiUrl();
            callRequest(apiString);

        }
private  callRequest(String apiString) {
        // TODO Auto-generated method stub

        request = new StringRequest(Request.Method.GET, apiString,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        // TODO Auto-generated method stub


                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub

                    }
                });


        queue.add(request);

    }

推荐答案

Volley Lib不提供本机方法来检查请求是否已完成,该信息由Lib内部类中的私有属性保存. 您可以为此创建自己的控件.

The Volley Lib does not provide a native method to check whether the request has already been finalized, this information is kept by a private property in an inner class of Lib. You can create your own control for this.

当我需要此功能时,我实现了以下方法来访问通过反射设置的CurrentRequests.

When I needed this functionality i implemented the following method to access CurrentRequests set through reflection.

我使用一个lib来简化此任务. 镜像

I used a lib to facilitate this task. Mirror

public boolean isPendingToRequest( final Object tag ) {

    final Object mObject = new Mirror().on( this.requestQueue ).get().field( "mCurrentRequests" );

    final Set<Request<?>> mCurrentRequests = ( Set<Request<?>> ) mObject;

    for ( final Request<?> request : mCurrentRequests ) {

        Log.d( "tagIsPendingToRequest ", "tag: " + request.getTag() );

        if ( request.getTag().equals( tag ) ) {

            Log.d( "tagIsPendingToRequest ", "Pendingtag: " + request.getTag() + " mytag:" + tag );
            return true;
        }
    }

    return false;
}

但是它对我来说效率不高,因此我决定保留带有我的请求的HashMap引用并带有标志.

But it did not work efficiently for me, so I decided to keep a HashMap reference to all my request with a flag.

这篇关于如何检查Volley Request Queue是否为空?Request是否完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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