嵌套的VOLLEY GET请求方法未提供正确的结果

时间:2018-11-14 12:45:01

标签: android android-volley android-json get-request

我有两个嵌套的VOLLEY GET请求。第一个API请求为我提供了我用于调用另一个Web服务的id字段。问题是我从第一个请求获取ID序列的方式,它没有为第二个请求调用相同的序列,后者使用了第一个请求返回的ID字段。
这是我嵌套的VOLLEY GET请求。

private void getData() {
            final JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, ApiUrls.RESERVATION, null, new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject object = response.getJSONObject(i);
                              final Entry entry = newEntry();
                            String Id = object.getString("id");
                            entry.setId(Id);
                           ID_url = ApiUrls.DETAILS + Id + "/";
                            JsonObjectRequest foodieInfo = new JsonObjectRequest(Request.Method.GET, ID_url, null, new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response) {
                                  try {
                                                                              String firstName = response.getString("firstname");
                                        entry.setName(firstName);
                                        adapter.notifyDataSetChanged();
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                }
                            }) {
                                @Override
                                public Map<String, String> getHeaders() throws AuthFailureError {
                                    Map<String, String> headers = new HashMap<>();
                                    String auth = "JWT " + myToken;
                                    headers.put("Authorization", auth);
                                    headers.put("Content-Type", "application/json");
                                    return headers;
                                }
                            };
                            AppController.getInstance().addToRequestQueue(foodieInfo);

                            current.add(entry);
                            adapter = new ReservationAdapter(current, getActivity().getApplicationContext());
                            recyclerView.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headers = new HashMap<>();
                    String auth = "JWT " + myToken;
                    headers.put("Authorization", auth);
                    headers.put("Content-Type", "application/json");
                    return headers;
                }

                @Override
                protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
                    int status=response.statusCode;
                    if(status==200){
                    }
                    return super.parseNetworkResponse(response);
                }
            };
            AppController.getInstance().addToRequestQueue(request);
        }  

我在第一个请求中获得的ID应该只在嵌套JSON GET请求中使用。但是,它使用第一个请求返回的不同ID,并使用该序列调用另一个请求。如何解决呢?

0 个答案:

没有答案