我需要使用volley发送请求以检索memberID,然后将该成员身份ID传递到第二个volley请求以检索该成员的统计信息.
我有一个问题,我的第一个请求工作完美但第二个请求似乎在返回变量传递之前开始.任何人都知道如何在返回值之前阻止第二个请求启动?
解决方法:
你不能只是按顺序写每个请求并等待每个成功响应后执行每个请求…你必须在第一个服务响应中调用第二个请求…即
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 one rrorResponse(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 one rrorResponse(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
以上是内存溢出为你收集整理的android – 需要按顺序发送多个Volley请求全部内容,希望文章能够帮你解决android – 需要按顺序发送多个Volley请求所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)