需要按顺序发送多个Volley请求

Ran*_*ndy 9 android android-volley

我需要使用volley发送请求以检索memberid,然后将该成员身份ID传递到第二个volley请求以检索该成员的统计信息.

我有一个问题,我的第一个请求工作完美但第二个请求似乎在返回变量传递之前开始.任何人都知道如何在返回值之前阻止第二个请求启动?

Ang*_*ari 10

你不能只是按顺序写每个请求并等待每个成功响应后执行每个请求...你必须在第一个服务响应中调用第二个请求...即

public void firstServiceCall(String url)
{
      JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                     int membershipid=response.getInt("membershipid");
                     //suppose the membershipid comes under main json with key "membershipid"
                     secondServiceCall(membershipid,<url2>);
                     // on the response of first service ... call to the second service ... and continue so on... if required
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    Volley.newRequestQueue(getApplicationContext()).add(jsonObjReq);
  }
  public void secondServiceCall(int membershipid,String url)
  {
       // use this var membershipid acc to your need ... 
       JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    Volley.newRequestQueue(getApplicationContext()).add(jsonObjReq);
  }
Run Code Online (Sandbox Code Playgroud)

因此请求调用是异步的...其他进程不会等待服务调用完成...因此您的第二个服务在第一个服务响应之前启动