0

I can parse json from an url in this way:

String link1 = "http://www.url.com/test1.json";
String link2 = "http://www.url.com/test2.json";
private void fetchMovies() {
    String url = link1;
    JsonArrayRequest req = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    if (response.length() > 0) {
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject movieObj = response.getJSONObject(i);
                                int rank = movieObj.getInt("rank");
                                String title = movieObj.getString("title");
                                Movie m = new Movie(rank, title);
                                movieList.add(0, m);
                            } catch (JSONException e) {
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
    MyApplication.getInstance().addToRequestQueue(req);
}

I want to parse my json from multiple url.

I want to parse url1 and url2 at the same time.

How can I do that?

5
  • There is an object inside a JSON object, not an array. Commented Jun 27, 2017 at 10:09
  • @Dennis I have updated my post. Can you check it? Commented Jul 18, 2017 at 19:22
  • Hi, you can't parse two JSON objects at the same time, but you can parse them consecutively(one after another). Well, I can't think of any hypothetical(imaginary) situation where you'll need to parse two JSON objects and save them into one class. If you'll work in an industry, you'll realize that the employee from the server end will prepare the JSON accordingly so you don't need to parse two JSON at the same time. Commented Jul 18, 2017 at 19:33
  • @Dennis I use this tutorial androidhive.info/2015/05/… I have more than 10 links and all json have the same structure. Commented Jul 18, 2017 at 20:03
  • oh hahaha, dude, they are two separate JSONs You need to parse them separately. One has a JSON ARRAY` and other usually has JSON OBJECT Commented Jul 19, 2017 at 9:53

1 Answer 1

1

Why don't you use "links" as array ? In case you will use an array:

JSONObject jsonObject = new JSONObject();
JSONArray keys = jsonObject.getJSONArray("links");
int length = keys.length();
for (int i = 0; i < length; i++) {
    new ReadJSON().execute(keys.getString(i));
}

Anyway, you take all the keys and go one after the other, and then query each EDIT:

JSONObject jsonObject = new JSONObject(/*Your links json*/);
JSONObject links = jsonObject.get("links");
Iterator<String> keys = links.keys();
while (keys.hasNext()) {
    new ReadJSON().execute(links.getString(keys.next()));
}
Sign up to request clarification or add additional context in comments.

9 Comments

There is an object inside a JSON object, not an array. Shouldn't it be "links:[ .... ] ?
@Dennis I agree that it is better to use links, I asked that first in my comment. I assumed that he has a good reason for that :) In case he will change it to links, he can use my code just go over the JSONArray values
Hi @Dennis Thanks for your help. I got this issue "cannot resolve method 'execute(java.lang.object)' " when I try your code.
You changed the asynctask name to ReadJSON2
Ahh sorry @fbwnd , I tried your code and I got issue. I have updated my post, please check it. Thank you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.