简体   繁体   English

从 Android Volley 中的多个 URL 中提取 JSON

[英]Extract JSON from multiple URLs in Android Volley

I am currently using Volley to extract JSON contents using the following code.我目前正在使用 Volley 使用以下代码提取 JSON 内容。

JsonArrayRequest servicesStatus = new JsonArrayRequest(url1,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            // Having obj to process further
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();

        }
    });

Now, I want one more JSON handler for the new URL and the dialog to be closed once it has successfully downloaded from both the URLs.现在,我想要新 URL 的另外一个 JSON 处理程序,并且一旦从两个 URL 成功下载,就关闭对话框。

I tried to copy paste the above thing with url1 replaced by url2 and different jsonarrayrequest name.我试图复制粘贴上面的东西,将 url1 替换为 url2 和不同的 jsonarrayrequest 名称。 And added the hideDialog() in the second one.并在第二个中添加了 hideDialog()。 But the second one is not being called at all.但是第二个根本没有被调用。

If you want make multiple request then you will have to add your Request to Queue .如果您想发出多个请求,则必须将您的请求添加到 Queue You can do it like this:你可以这样做:

   RequestQueue request =  Volley.newRequestQueue(Context);
   request.add(FirstRequest);
   request.add(SecondRequest);

This should help you to add multiple request in Volley.这应该可以帮助您在 Volley 中添加多个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM