需要发送多个排球请求-按顺序 [英] Need to send multiple Volley Requests - in a sequence

查看:52
本文介绍了需要发送多个排球请求-按顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用齐射发送请求以检索会员ID,然后将该会员ID传递给第二个齐射请求以检索该成员的统计信息.

I need to use volley to send a request to retrieve a membershipid then pass that membership id into the second volley request to retrieve stats on that member.

我的第一个请求运行正常时遇到问题,但是第二个请求似乎在变量被返回传递之前开始.有谁知道如何防止第二个请求在返回值之前启动?

I have a problem with my first request working perfectly but the second request seems to start before the variable is returned to be passed. Anyone know how to prevent the second request from starting before value is returned?

推荐答案

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

you can't just write each request sequentially and wait to perform each after each success response ... you have to call second request inside first service response... ie

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

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

also the request call is asynchronous hence... the other process won't wait for service call to finish...hence your second service starts before the first service response

这篇关于需要发送多个排球请求-按顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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