Je peux voir le JSON dans LOGCAT après l'avoir récupéré, mais il n'est pas affiché dans mon activité. Il continue de se charger ...

Le lien pour mon service Web est: "http://dcafe-menu.getsandbox.com/dcafe -menu-vendredi "

**<-- My FridayMenu.java is:  ->>**
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;
        }
    }
}

** Et j'obtiens ce qui suit dans LOGCAT **

Je peux voir le json dans logcat mais il ne se charge pas dans mon application. Et ce qui suit peut être vu dans le logcat. Lorsque mon activité démarre, elle continue de se charger. J'ai attendu 15 minutes. Je n'ai toujours pas pu voir le résultat souhaité dans mon 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)
-1
Nishant Sharma 30 nov. 2017 à 01:11

3 réponses

Meilleure réponse

Faites deux choses pour cela:

Dans tous les objets de récupération Json au lieu d'utiliser "get", utilisez "opt" ici:

  String DRY_CEREAL = c.optString(TAG_DRY_CEREAL);

Et deuxièmement, là où vous mettez hashmap, utilisez ceci:

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

Faites ceci pour chaque entrée de hashmap. Cela résoudra votre problème.

1
halfer 29 nov. 2017 à 22:24

La raison pour laquelle il continue à se charger est que vous ne masquez pas la barre de progression.

@Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        pDialog.hide();
    }
0
kukroid 29 nov. 2017 à 22:26

Vous devez utiliser "opt" au lieu de "get" avec la valeur de secours par défaut

Voir Voici la différence entre eux:

getString(String name)

Returns the value mapped by name if it exists, coercing it if necessary, or throws if no such mapping exists. 

 optString(String name, String fallback)

 Returns the value mapped by name if it exists, coercing it if necessary, or fallback if no such mapping exists. 

Par exemple comme dans votre cas:

Courant

  String L_SALAD = c.getString(TAG_L_SALAD);

Remplacer par :

  String L_SALAD = c.optString(TAG_L_SALAD,"");

Faites de même avec chacun, j'espère que votre problème sera résolu.

1
guptaatul91 30 nov. 2017 à 04:04
47562707