我可以在获取后在LOGCAT中看到JSON,但它在我的活动中没有显示。它继续加载...
我的网络服务的链接是:“http://dcafe-menu.getsandbox.com/dcafe-menu-friday”
**<-- 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;
}
}
}
**我在LOGCAT **中得到以下内容
我可以在logcat中看到json,但它没有加载到我的应用程序中。并且在logcat中可以看到以下内容。当我的活动开始时,它会继续加载。我等了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)
答案 0 :(得分:1)
为此做两件事:
在所有Json提取对象而不是使用“get”中使用“opt”:
String DRY_CEREAL = c.optString(TAG_DRY_CEREAL);
其次,在你放入hashmap的地方使用它:
If(DRY_CEREAL==null||DRY_CEREAL.isEmpty())
{
fridaymenu.put(TAG_DRY_CEREAL, " ");
}
else
{
fridaymenu.put(TAG_DRY_CEREAL, DRY_CEREAL);
}
为每个hashmap条目执行此操作。它会解决你的问题。
答案 1 :(得分:1)
您必须使用“opt”代替“get”并使用默认回退值
见这是他们之间的区别:
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.
例如,在您的情况下:
当前
String L_SALAD = c.getString(TAG_L_SALAD);
替换为:
String L_SALAD = c.optString(TAG_L_SALAD,"");
对每个人都这样做。我希望你的问题能得到解决。
答案 2 :(得分:0)
它继续加载的原因是,您没有隐藏进度条。
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pDialog.hide();
}