JSON parsing for my Android application development project

Issue

I can see the JSON in logcat after it is fetched, but it is not shown in my activity. It keeps on loading.

Link for my webservice is: "http://dcafe-menu.getsandbox.com/dcafe-menu-friday"

FridayMenu.java:

package com.example.nishant.messmenu;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class FridayMenu extends AppCompatActivity {

    private ProgressDialog pDialog;

    // JSON Node names
    private static final String TAG_FRIDAY = "fridaymenu";
    private static final String TAG_DRY_CEREAL = "DRY_CEREAL";
    private static final String TAG_DRINK_ONE = "DRINK_ONE";
    private static final String TAG_DRINK_TWO = "DRINK_TWO";
    private static final String TAG_MAIN = "MAIN";
    private static final String TAG_ACCOMPANIMENT = "ACCOMPANIMENT";
    private static final String TAG_B_BREAD = "B_BREAD";
    private static final String TAG_L_SALAD = "L_SALAD";
    private static final String TAG_L_RICE = "L_RICE";
    private static final String TAG_L_DAL = "L_DAL";
    private static final String TAG_L_PANEER = "L_PANEER DISH";
    private static final String TAG_L_SEMI = "L_SEMI DRY VEG";
    private static final String TAG_L_GRAVY = "L_GRAVY VEGETABLE";
    private static final String TAG_L_DESSERT = "L_DESSERT";
    private static final String TAG_L_BREAD = "L_BREAD";
    private static final String TAG_L_CURD = "L_CURD";
    private static final String TAG_SNACKS = "SNACKS";
    private static final String TAG_DRINK = "DRINK";
    private static final String TAG_D_SALAD = "D_SALAD";
    private static final String TAG_D_RICE = "D_RICE";
    private static final String TAG_D_DAL = "D_DAL";
    private static final String TAG_D_PANEER = "D_PANEER DISH";
    private static final String TAG_D_SEMI = "D_SEMI DRY VEG";
    private static final String TAG_D_GRAVY = "D_GRAVY VEGETABLE";
    private static final String TAG_D_DESSERT = "D_DESSERT";
    private static final String TAG_D_BREAD = "D_BREAD";
    private static final String TAG_D_CURD = "D_CURD";



    // contacts JSONArray
    JSONArray fridaymenu = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friday_menu);


        // Calling async task to get json
        new GetContacts().execute();
    }


    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(FridayMenu.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String url = "http://dcafe-menu.getsandbox.com/dcafe-menu-friday";
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    // Getting JSON Array node
                    fridaymenu = jsonObj.getJSONArray(TAG_FRIDAY);


                    // looping through All Contacts
                    for (int i = 0; i < fridaymenu.length(); i++) {
                        JSONObject c = fridaymenu.getJSONObject(i);

                        String DRY_CEREAL = c.getString(TAG_DRY_CEREAL);
                        String DRINK_ONE = c.getString(TAG_DRINK_ONE);
                        String DRINK_TWO = c.getString(TAG_DRINK_TWO);
                        String MAIN = c.getString(TAG_MAIN);
                        String ACCOMPANIMENT = c.getString(TAG_ACCOMPANIMENT);
                        String B_BREAD = c.getString(TAG_B_BREAD);
                        String L_SALAD = c.getString(TAG_L_SALAD);
                        String L_RICE = c.getString(TAG_L_RICE);
                        String L_DAL = c.getString(TAG_L_DAL);
                        String L_PANEER = c.getString(TAG_L_PANEER);
                        String L_SEMI = c.getString(TAG_L_SEMI);
                        String L_GRAVY = c.getString(TAG_L_GRAVY);
                        String L_DESSERT = c.getString(TAG_L_DESSERT);
                        String L_BREAD = c.getString(TAG_L_BREAD);
                        String L_CURD = c.getString(TAG_L_CURD);
                        String SNACKS = c.getString(TAG_SNACKS);
                        String DRINK = c.getString(TAG_DRINK);
                        String D_SALAD = c.getString(TAG_D_SALAD);
                        String D_RICE = c.getString(TAG_D_RICE);
                        String D_DAL = c.getString(TAG_D_DAL);
                        String D_PANEER = c.getString(TAG_D_PANEER);
                        String D_SEMI = c.getString(TAG_D_SEMI);
                        String D_GRAVY = c.getString(TAG_D_GRAVY);
                        String D_DESSERT = c.getString(TAG_D_DESSERT);
                        String D_BREAD = c.getString(TAG_D_BREAD);
                        String D_CURD = c.getString(TAG_D_CURD);


                        // tmp hashmap for single contact
                        HashMap<String, String> fridaymenu = new HashMap<String, String>();


                        // adding each child node to HashMap key => value
                        fridaymenu.put(TAG_DRY_CEREAL, DRY_CEREAL);
                        fridaymenu.put(TAG_DRINK_ONE, DRINK_ONE);
                        fridaymenu.put(TAG_DRINK_TWO, DRINK_TWO);
                        fridaymenu.put(TAG_MAIN, MAIN);
                        fridaymenu.put(TAG_ACCOMPANIMENT, ACCOMPANIMENT);
                        fridaymenu.put(TAG_B_BREAD, B_BREAD);
                        fridaymenu.put(TAG_L_SALAD, L_SALAD);
                        fridaymenu.put(TAG_L_RICE, L_RICE);
                        fridaymenu.put(TAG_L_DAL, L_DAL);
                        fridaymenu.put(TAG_L_PANEER, L_PANEER);
                        fridaymenu.put(TAG_L_SEMI, L_SEMI);
                        fridaymenu.put(TAG_L_GRAVY, L_GRAVY);
                        fridaymenu.put(TAG_L_DESSERT, L_DESSERT);
                        fridaymenu.put(TAG_L_BREAD, L_BREAD);
                        fridaymenu.put(TAG_L_CURD, L_CURD);
                        fridaymenu.put(TAG_SNACKS, SNACKS);
                        fridaymenu.put(TAG_DRINK, DRINK);
                        fridaymenu.put(TAG_D_SALAD, D_SALAD);
                        fridaymenu.put(TAG_D_RICE, D_RICE);
                        fridaymenu.put(TAG_D_DAL, D_DAL);
                        fridaymenu.put(TAG_D_PANEER, D_PANEER);
                        fridaymenu.put(TAG_D_SEMI, D_SEMI);
                        fridaymenu.put(TAG_D_GRAVY, D_GRAVY);
                        fridaymenu.put(TAG_D_DESSERT, D_DESSERT);
                        fridaymenu.put(TAG_D_BREAD, D_BREAD);
                        fridaymenu.put(TAG_D_CURD, D_CURD);

                    }

                }
                catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }
    }
}

I can see the JSON in logcat but it isn’t loading in my app. And the following can be seen in the logcat. When my activity starts it keeps on loading. I’ve waited for 15 minutes. Still I couldn’t see the desired result in my application:

11-30 03:10:32.041 9973-10198/com.example.nishant.messmenu W/System.err: org.json.JSONException: No value for L_SALAD
11-30 03:10:32.070 9973-10198/com.example.nishant.messmenu W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
11-30 03:10:32.071 9973-10198/com.example.nishant.messmenu W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
11-30 03:10:32.072 9973-10198/com.example.nishant.messmenu W/System.err:     at com.example.nishant.messmenu.FridayMenu$GetContacts.doInBackground(FridayMenu.java:109)
11-30 03:10:32.072 9973-10198/com.example.nishant.messmenu W/System.err:     at com.example.nishant.messmenu.FridayMenu$GetContacts.doInBackground(FridayMenu.java:68)
11-30 03:10:32.072 9973-10198/com.example.nishant.messmenu W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:304)
11-30 03:10:32.072 9973-10198/com.example.nishant.messmenu W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-30 03:10:32.072 9973-10198/com.example.nishant.messmenu W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
11-30 03:10:32.072 9973-10198/com.example.nishant.messmenu W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
11-30 03:10:32.072 9973-10198/com.example.nishant.messmenu W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
11-30 03:10:32.072 9973-10198/com.example.nishant.messmenu W/System.err:     at java.lang.Thread.run(Thread.java:761)

Solution

Just do two thing for this:

In all Json fetching objects instead of using “get” use “opt” there:

  String DRY_CEREAL = c.optString(TAG_DRY_CEREAL);

And secondly, where you put in hashmap use this:

 If(DRY_CEREAL==null||DRY_CEREAL.isEmpty())
 {
        fridaymenu.put(TAG_DRY_CEREAL, " ");
 }
 else
 {
        fridaymenu.put(TAG_DRY_CEREAL, DRY_CEREAL);
 }

Do this for every hashmap entry. It will solve your problem.

Answered By – Ankit Patidar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published