我可以在logcat中看到获取的JSON,但它没有显示在我的Activity中,它一直在加载。
我的Web服务的链接是:“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;
}
}
}
我可以在logcat中看到JSON,但它没有在我的应用程序中加载。在logcat中可以看到以下内容。当我的Activity开始时,它一直在加载。我已经等了15分钟。但我仍然无法在我的应用程序中看到所需的结果:
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)
3条答案
按热度按时间pu3pd22g1#
为此只需做两件事:
在所有Json获取对象时,不要使用“get”,而要使用“opt”:
第二,在hashmap中使用以下代码:
对每个散列表条目都这样做,它将解决您的问题。
velaa5lx2#
必须使用“opt”代替“get”,并使用默认回退值
这是他们之间的区别:
例如在你的情况:
电流
改为:
对每个人都做同样的事情。我希望你的问题会得到解决。
t2a7ltrp3#
它持续加载的原因是,你没有隐藏进度条。