Android截击-在所有请求完成时获取回调

Android截击-在所有请求完成时获取回调,android,Android,我很感激你能给我的任何帮助。我有以下问题: 我有一个应用程序,它使用SyncAdapter定期和手动地将本地数据库与服务器数据同步。但我需要的是,当用户刚要登录到应用程序时,会执行手动同步,同时会显示一个加载对话框。同步完成后,应隐藏对话框,并显示主要活动。你推荐什么 我正在使用截击来处理HTTP请求。我有点困惑,因为请求总是异步运行,所以很难知道所有请求何时完成以隐藏对话框 代码如下: VolleySingleton.getInstance(getContext()).addToRequest

我很感激你能给我的任何帮助。我有以下问题:

我有一个应用程序,它使用SyncAdapter定期和手动地将本地数据库与服务器数据同步。但我需要的是,当用户刚要登录到应用程序时,会执行手动同步,同时会显示一个加载对话框。同步完成后,应隐藏对话框,并显示主要活动。你推荐什么

我正在使用截击来处理HTTP请求。我有点困惑,因为请求总是异步运行,所以很难知道所有请求何时完成以隐藏对话框

代码如下:

VolleySingleton.getInstance(getContext()).addToRequestQueue(
        new JsonObjectRequest(
            Request.Method.GET,
            Constants.GET_URL,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    // This method sync the data
                    updateLocalData(response, syncResult);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    getContext().sendBroadcast(new Intent(Constants.SYNC_CORRUPTED_BY_SERVER));
                    Log.d(TAG, "Sync (makeLocalSync), Exception => " +
                            error.getLocalizedMessage());
                }
            }
        )
    );
对不起,我的英语水平


谢谢

不幸的是,Volley的RequestQueue没有提供任何接口或回调来让您知道所有挂起的请求何时完成。我发现在所有请求完成时要通知的一个解决方法是为每个请求的响应或失败更新一个int和一个boolean类成员。重要的是,您的请求可以访问这些成员,并且该数字对应于更新的值,因此当该值下降到0时,您将知道所有请求都已完成

private int numberOfRequestsToMake = NUMBER;
private boolean hasRequestFailed = false; 
然后,在创建请求时:

JsonObjectRequest req = new JsonObjectRequest(url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "successful request to " + url);
                    numberOfRequestsToMake--;

                    if(numberOfRequestsToMake == 0) {
                        if(!hasRequestFailed) {
                            //All requests finished correctly
                        } else {
                            //At least one request failed
                        }
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "unsuccessful request to " + url);
                    numberOfRequestsToMake--;
                    hasRequestFailed = true;

                    if(numberOfRequestsToMake == 0) {
                        //The last request failed
                    }
                }
            }
    );

    MyVolleySingleton.getInstance().addToRequestQueue(req);

我已经解决了我的问题如下:

VolleySingleton.getInstance(getContext()).addToRequestQueue(
        new JsonObjectRequest(
            Request.Method.GET,
            Constants.GET_URL,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    // This method sync the data
                    updateLocalData(response, syncResult);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    getContext().sendBroadcast(new Intent(Constants.SYNC_CORRUPTED_BY_SERVER));
                    Log.d(TAG, "Sync (makeLocalSync), Exception => " +
                            error.getLocalizedMessage());
                }
            }
        )
    );
在n个截击请求中的每个请求的方法Response.Listener和Respones.ErrorListener中,我使用一个计数器和一个标志来处理广播,如下所示:

VolleySingleton.getInstance(getContext()).addToRequestQueue(
        new JsonObjectRequest(
            Request.Method.GET,
            Constants.GET_URL,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    // This method sync the data
                    updateLocalData(response, syncResult);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    getContext().sendBroadcast(new Intent(Constants.SYNC_CORRUPTED_BY_SERVER));
                    Log.d(TAG, "Sync (makeLocalSync), Exception => " +
                            error.getLocalizedMessage());
                }
            }
        )
    );
如果计数器的值等于请求数,则所有请求都已完成,然后使用标志检查是否有任何请求出现问题

代码如下:

VolleySingleton.getInstance(getContext()).addToRequestQueue(
        new JsonObjectRequest(
            Request.Method.GET,
            Constants.GET_URL,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    // This method sync the data
                    updateLocalData(response, syncResult);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    getContext().sendBroadcast(new Intent(Constants.SYNC_CORRUPTED_BY_SERVER));
                    Log.d(TAG, "Sync (makeLocalSync), Exception => " +
                            error.getLocalizedMessage());
                }
            }
        )
    );
请求n1:

    // Counter and flag to handle requests
    private static int iCountRequest;
    private static boolean bAnyErrors;
    // Before initiating all the requests, the variables must be restarted
    iCountRequest = 0;
    bAnyErrors = false;
    // Request # n1:
    VolleySingleton.getInstance(getContext()).addToRequestQueue(
            new JsonObjectRequest(
                    Request.Method.GET,
                    Constants.REQUEST_N1_URL,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            iCountRequest++;
                            // Response code..
                            sendBroadcastToActivity();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            iCountRequest++;
                            bAnyErrors = true;
                            sendBroadcastToActivity();
                        }
                    }
            )
    );
截击单打:

public final class VolleySingleton {

    private static VolleySingleton oSingleton;
    private RequestQueue oRQ;
    private static Context oContext;

    private VolleySingleton(Context context) {
        VolleySingleton.oContext = context;
        oRQ = getRequestQueue();
    }

    public static synchronized VolleySingleton getInstance(Context context) {
        if (oSingleton == null) {
            oSingleton = new VolleySingleton(context.getApplicationContext());
        }
        return oSingleton;
    }

    private RequestQueue getRequestQueue() {
        if (oRQ == null) {
            oRQ = Volley.newRequestQueue(oContext.getApplicationContext());
        }
        return oRQ;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

}
我希望它对你有用。谢谢@FerDensetsu

我使用以下代码:

queue.addRequestFinishedListener(request -> {
        countRequests++;
        if (countRequests == 2) {
            Log.d("tag", "finished");
        }
    });

考虑到以后有N个请求。其他n截击Singleton.getInstance getContext。AddToRequestQueue。。。