如何在Android中使用Volley同时从多个网址获取数据?

时间:2017-12-26 11:40:49

标签: android android-volley

我知道怎么做但问题是如果第一个请求成功获取数据并从第一个凌空请求获取数据,我想获取数据。我不知道如何使用Volley从第二个URL获取数据的第一个请求的成功响应,并行排列意外请求。

这是我的代码。

public class BlankHomeFragment extends Fragment {
    String url24, name;
    RecyclerView recycler;
    StringRequest autherRequest;
    Context context;
    String originalName = null, originalImage = null;
    ArrayList<String> mainList, loadList;

    String serverURL = "https://www.google.com/";   //for example site

    public BlankHomeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context; //make context on fragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_blank_home, container, false);
        recycler = view.findViewById(R.id.recycler);

        recycler.setLayoutManager(new LinearLayoutManager(context));
        //make item divider
        DividerItemDecoration decoration = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
        recycler.addItemDecoration(decoration);
        recycler.setHasFixedSize(true);

        LoadRecyclDataFromServer();


        // recycler.setAdapter();
        return view;
    }

    private void LoadRecyclDataFromServer() {
        final ArrayList<String> oauth = new ArrayList<>();
        final ArrayList<String> ourl = new ArrayList<>();
        StringRequest request = new StringRequest(Request.Method.GET, serverURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
             //you can simply get Title and description
                try {
                    //root element is JsonArray.
                    JSONArray jsarr = new JSONArray(response);
                    Log.i("response", response);
                    String title = null, auth = null, date = null,desc=null;
                    for (int i = 0; i < jsarr.length(); i++) {
                        JSONObject jsonObject = jsarr.getJSONObject(i);
                        //get id using id keys.
                        String id = jsonObject.getString("id");
                        Log.i("ID ", "" + id);

                        auth = jsonObject.getString("_links");
                        date = jsonObject.getString("date");
                        JSONObject jsonObject1 = jsonObject.getJSONObject("title");
                        title = jsonObject1.getString("rendered");

                        //get description.
                        JSONObject jsonDesc = jsonObject.getJSONObject("content");
                        desc=jsonDesc.getString("rendered");

                        //get Author values OR data.
                        JSONObject jsLinkArr = new JSONObject(auth);
                        String auther = jsLinkArr.get("author").toString();
                        JSONArray jsonArray = new JSONArray(auther);
                        for (int i1 = 0; i1 < jsonArray.length(); i1++) {
                            JSONObject jsonObject2 = jsonArray.getJSONObject(i1);
                            //getting URL.
                            final String url = jsonObject2.getString("href");
                            Log.i("JsonLinkArrayAuth", url);

                            //below code will fetch the auther data from given url.
                            autherRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonAUthDetails = new JSONObject(response);
                                        String id;
                                        id = jsonAUthDetails.getString("id");
                                        name = jsonAUthDetails.getString("name");
                                        //get user image url.
                                        JSONObject jsonURL = jsonAUthDetails.getJSONObject("avatar_urls");
                                        url24 = jsonURL.getString("24");

                                        String url48, url96;
                                        url48 = jsonURL.getString("48");
                                        url96 = jsonURL.getString("96");

                                        Log.e("URL", url24 + "\nURL48" + url48 + "\nURL96" + url96);
                                        //get name here.
                                        oauth.add(name);
                                        ourl.add(url24);
                                        Log.i("ResponseAuth", "\nID" + id + "\nName " + name);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    //handle Error.

                                }
                            });
                        }
                        RequestQueue requestQueue = Volley.newRequestQueue(context);
                        requestQueue.add(autherRequest);
                        autherRequest.setRetryPolicy(
                                new DefaultRetryPolicy(1 * 1000, 1, 2.0f));
                        Log.i("Responsedata", "\nAuthor : " + auth + "\nDate " + date + "\nTitle : " + title +"\nDescription"+desc+ "\nAuthName " + oauth  + "\nAuthURL " + ourl);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener()

        {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        requestQueue.add(request);
        Log.i("ResponsedataAuth", "\nAuthName " + oauth.toString() + "\nAuthURL " + ourl.toString());
    }
}

0 个答案:

没有答案