Search code examples
androidandroid-asynctaskgetjson

org.json.JSONException: No value for 0


I'm getting response from an API. But as soon as I get to the return command I get the following error:

W/System.err: org.json.JSONException: No value for 0
W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
W/System.err:     at org.json.JSONObject.getInt(JSONObject.java:478)
W/System.err:     at bobbydejong.expandabletest.OrderOverzicht.prepareOrderList(OrderOverzicht.java:65)
W/System.err:     at bobbydejong.expandabletest.OrderOverzicht$1.onSuccess(OrderOverzicht.java:37)
W/System.err:     at bobbydejong.expandabletest.GetOrderSwagger.onPostExecute(GetOrderSwagger.java:113)
W/System.err:     at bobbydejong.expandabletest.GetOrderSwagger.onPostExecute(GetOrderSwagger.java:21)
W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:660)
W/System.err:     at android.os.AsyncTask.-wrap1(AsyncTask.java)

It happens on this piece of code:

public class GetItemsSwagger extends AsyncTask<String, Void, String> {

ArrayList<HashMap<String, Object>> list = new ArrayList<>();
String result2;
private OnEventListener<String> mCallBack;
public Exception mException;
private Context context;
HttpURLConnection conn = null;
OutputStream os = null;
InputStream is = null;
String toastMsg = "Failed to get API response.";
String apiKey;




public GetItemsSwagger(Context context, OnEventListener callback) {
    this.context = context;
    this.mCallBack = callback;
}

@Override
protected String doInBackground(String... strings) {



    try {
        apiKey = strings[1];

        URL url = new URL(strings[0]);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setInstanceFollowRedirects(false);
        conn.setRequestMethod("GET");
        conn.setUseCaches(false);
        conn.setRequestProperty("apikey", apiKey);

        int status = conn.getResponseCode();

        if(status >= 400)
            is = conn.getErrorStream();
        else
            is = conn.getInputStream();


        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        } else {
            toastMsg = "";
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(
                (is)));

        String output;
        while ((output = br.readLine()) != null) {
            toastMsg += output;
        }

        conn.disconnect();



        result2= "";
        return toastMsg;

EDIT:

[{"orderGroupID":"1","name":"Patat","orderStatusID":"8","orderTo":"2017-02-24 11:15:00","created":"2017-02-24 10:04:21"},{"orderGroupID":"2","name":"Patat","orderStatusID":"8","orderTo":"2017-03-03 11:00:00","created":"2017-03-03 10:06:53"},{"orderGroupID":"3","name":"Patat","orderStatusID":"8","orderTo":"2017-03-10 11:10:00","created":"2017-03-10 10:27:32"},{"orderGroupID":"4","name":"Patat","orderStatusID":"8","orderTo":"2017-03-17 11:00:00","created":"2017-03-17 08:27:48"},{"orderGroupID":"5","name":"Patat","orderStatusID":"8","orderTo":"2017-03-24 11:00:00","created":"2017-03-24 08:58:08"},{"orderGroupID":"6","name":"Patat","orderStatusID":"8","orderTo":"2017-03-31 11:00:00","created":"2017-03-31 09:26:01"},{"orderGroupID":"7","name":"Patat","orderStatusID":"8","orderTo":"2017-04-07 11:00:00","created":"2017-04-07 10:02:52"},{"orderGroupID":"8","name":"Patat","orderStatusID":"8","orderTo":"2017-04-14 11:00:00","created":"2017-04-14 09:40:43"},{"orderGroupID":"9","name":"Patat","orderStatusID":"8","orderTo":"2017-04-21 11:00:00","created":"2017-04-21 10:01:31"},{"orderGroupID":"11","name":"Patat","orderStatusID":"8","orderTo":"2017-04-28 11:00:00","created":"2017-04-28 10:12:45"},{"orderGroupID":"12","name":"Patat","orderStatusID":"8","orderTo":"2017-05-05 11:00:00","created":"2017-05-05 09:39:56"},{"orderGroupID":"13","name":"Patat","orderStatusID":"8","orderTo":"2017-05-12 11:10:00","created":"2017-05-12 09:52:26"},{"orderGroupID":"14","name":"Patat","orderStatusID":"8","orderTo":"2017-05-19 11:00:00","created":"2017-05-19 09:58:37"}]

This is the JSON response

 try {
        JSONArray jObjArray = new JSONArray(orderResult);
        int length = jObjArray.length();
        final ArrayList<String> orderList = new ArrayList<String>();

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

            HashMap<String, Object> map = new HashMap<>();
            JSONObject jObj = jObjArray.getJSONObject(i);
            map.put("orderGroupID", jObj.getInt("orderGroupID"));
            map.put("0", jObj.getInt("0"));
            map.put("name", jObj.getString("name"));
            map.put("orderStatusID", jObj.getInt("orderStatusID"));
            map.put("2", jObj.getInt("2"));
            map.put("orderTo", jObj.getString("orderTo"));
            map.put("3", jObj.getString("3"));
            map.put("created", jObj.getString("created"));


            String orderTo = jObj.getString("orderTo");
            String orderGroupID = jObj.getString("orderGroupID");


            orderList.add(orderTo+"   "+orderGroupID);

This is where I manipulate the code (different class)

I am looking forward to your response, I've been trying to find this problem on other posts but it seems those solutions don't workout for me and not quite the same problem. Correct me if I'm wrong but I'm a starter!


Solution

  • First, "orderGroupID" value within your JSON response is a String and you are trying to access it via jObj.getInt("orderGroupID"));. You should access it using jObj.getString("orderGroupID"));

    Second, there is not any Key with value "0" within your JSON response, so that parsing will throw and Exception.