1

In my android app I want to fetch JSON data from a website. I created it to fetch data with this structure:

[{"id":"1","name":"a"},{"id":"2","name":"a"},{"id":"3","name":"a"},{"id":"4","name":"a"}]

It works perfectly like it should. Now I tried to manipulate the code a little to fetch other JSON data.it has this structure:

{"ok":true,"license":"xx","data":"zz","status":"ok","stations":
[{"id":"474","name":"ABC","street":"MARGARETE-STR."},
{"id":"442","name":"XYZ","street":"ABCSTR"}]}

I don‘t get any results for it, I thought it might be because the data that should be fetched lays in an array, but I thought as well that I am getting it anyway. Am I fetching it the wrong way?

My code:

public class MainActivity extends AppCompatActivity {

    List<GetDataAdapter> GetDataAdapter1;

    RecyclerView recyclerView;
    RecyclerView.LayoutManager recyclerViewlayoutManager;
    RecyclerView.Adapter recyclerViewadapter;
    ProgressBar progressBar;
    String GET_JSON_DATA_HTTP_URL = "xx.com";
    String JSON_ID = "id";
    String JSON_NAME = "name";
    String JSON_SUBJECT = "email";
    String JSON_STREET = "street";
    Button button;
    JsonArrayRequest jsonArrayRequest;
    com.android.volley.RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        GetDataAdapter1 = new ArrayList<>();
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        button = (Button) findViewById(R.id.button);
        recyclerView.setHasFixedSize(true);
        recyclerViewlayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(recyclerViewlayoutManager);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressBar.setVisibility(View.VISIBLE);
                JSON_DATA_WEB_CALL();
            }
        });
    }

    public void JSON_DATA_WEB_CALL() {

        jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,

                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        progressBar.setVisibility(View.GONE);
                        JSON_PARSE_DATA_AFTER_WEBCALL(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonArrayRequest);
    }

    public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array) {

        for (int i = 0; i < array.length(); i++) {

            GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

            JSONObject json = null;
            try {
                json = array.getJSONObject(i);

                GetDataAdapter2.setId(json.getInt(JSON_ID));
                GetDataAdapter2.setName(json.getString(JSON_NAME));
                GetDataAdapter2.setStreet(json.getString(JSON_STREET));

            } catch (JSONException e) {

                e.printStackTrace();
            }
            GetDataAdapter1.add(GetDataAdapter2);
        }
        recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
        recyclerView.setAdapter(recyclerViewadapter);
    }
}
5
  • 1
    The first object is JSONObject and not a JSONArray, you can get array with jsonObject.getJSONArray("stations"). PLEASE, use Java naming conventions like everywhere you broke it making your code harder to read. Commented Jun 13, 2018 at 17:57
  • Thank you, for the advice. Would I call the specific object get-method in onResponse then? Can‘t I use it within my existing ArrayRequest? Commented Jun 13, 2018 at 18:18
  • No, you have to use JsonObjectRequest as stated in the below answer, then you will receive onResponse(JSONObject obj) this obj contains the fields (ok, licence, and others) and 'stations' will be a field of the object that IS a JSONArray Commented Jun 13, 2018 at 18:21
  • Ah so I need to handle it as an object to even get the 'station' wich then I can use as an array like I did before? Commented Jun 13, 2018 at 18:25
  • Exactly, you get a JSONObject the second sample you added and then you get a JSONArray from inside of it that it as field named 'stations' Commented Jun 13, 2018 at 18:38

1 Answer 1

1

The second JSON sample is not a JSON array; it is a JSON object. You should use a JsonObjectRequest instead of a JsonArrayRequest to obtain this data.

Sign up to request clarification or add additional context in comments.

3 Comments

These brackets { } are defining an Object right? I only want to fetch what‘s within [ ] these brackets, or is that the same problem that you mentioned?
Correct, {} indicates an object and [] indicates an array. You still need to handle the entire response, though. You need to read in the response as an object and then get the array you care about with response.getJSONArray("stations")
Thank you for showing me my problem, I found a solution!

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.