HDR
HDR

Reputation: 76

Handling Volley when no key exists

I have implemented Volley and Recycler view to parse and display a list of few items from a simple JSON file. There are at times when a key doesnot exists in an object but may appear in other object. That key is already defined using object.getInt("someKey").

As soon as Volley starts parsing the object with the missing key, it breaks out of the for loop (objects are stored in array) and catches the JSONException e, which is exactly what the app is supposed to do in case of a missing key.

However, I would like to prevent this behavior and use a placeholder value for that missing key of that particular object, so that the array list gets successfully buildup and recyclerview gets filled thereby application starts working normally.

What logic can I use in order to achieve this behavior?

Thank you!

private void parseJSON() {

    String url = "https://example.com/index.json";
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    try {
                        for (int i = 0; i < response.length(); i++) {
                            JSONObject object = response.getJSONObject(i);

                            String title = object.getString("subject");
                            String description = object.getString("message");
                            String imageUrl = object.getString("thumb");
                            Integer threadId = object.getInt("threadId");

                            mList.add(new Item( title, description, threadId));

                        }


                        mItemAdapter.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();

                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

In the above code, threadId is the key, which may or maynot appear in many objects of the JSON.

Upvotes: 0

Views: 568

Answers (3)

Kmelliti
Kmelliti

Reputation: 111

If i understood well your Question , here what you need to do , if you are not sure if this key exists use the following

jsonObject.optBoolean("yourKey");
        jsonObject.optInt("yourKey");
        jsonObject.optString("yourKey");
        jsonObject.optLong("yourKey");
        jsonObject.optDouble("yourKey");
        jsonObject.optJSONArray("yourKey");

this will make sure jsonObject will ignore that key if it doesn't exist.

Upvotes: 2

Balaji S B
Balaji S B

Reputation: 36

object.getInt("someKey") is to get value from "somekey".if somekey is not appeared it shows JSONException. Instead of this object.optInt("someKey"). It will get value from "somekey" if it appears, otherwise it skipped. This is simple solution. Thanks

Upvotes: 0

Ramesh Kumar
Ramesh Kumar

Reputation: 1249

You can check firstly if your key exists on object

 try {
        for (int i = 0; i < response.length(); i++) {
            JSONObject object = response.getJSONObject(i);

            String title = object.getString("subject");
            String description = object.getString("message");
            String imageUrl = object.getString("thumb");
            Integer threadId;
            if(object.toString().contain("threadId"){
                threadId = object.getInt("threadId");
            }else{
                threadId = 0;
            }
            mList.add(new Item( title, description, threadId));
        }
        mItemAdapter.notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }

Upvotes: 2

Related Questions