我正在做一个移动应用程序,它从我自己的API中检索信息。我正在尝试用JSON获取餐馆详细信息并解析它们以进行显示。这是我得到的错误:
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@cf94aa3 time:148477558
E/MainActivity: Response from url: {
"address1": "Market Square, Smithfield, Dublin Dublin 7",
"address2": "Dublin 7",
"cost": 35,
"lat": 53.3489980000,
"lng": -6.2788120000,
"menu_type": "BBQ",
"name": "My Meat Wagon",
"offer": "Meal for 10\u20ac",
"phone": 53463267,
"rate": 4.1
}
E/MainActivity: Json parsing error: No value for restaurants
D/ViewRootImpl: #3 mView = null
这是我正在使用的代码:
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray restaurants = jsonObj.getJSONArray("restaurants");
// looping through the JSON object
JSONObject c = restaurants.getJSONObject(0);
String name = c.getString("name");
String address1 = c.getString("address1");
String address2 = c.getString("address2");
String lat = c.getString("lat");
String lng = c.getString("lng");
String cost = c.getString("cost");
String menu_type = c.getString("menu_type");
String rate = c.getString("rate");
String offer = c.getString("offer");
// Phone node is JSON Object
String mobile = c.getString("mobile");
// tmp hash map for single restaurant
HashMap<String, String> restaurant = new HashMap<>();
// adding each child node to HashMap key => value
restaurant.put("name", name);
restaurant.put("address1", address1);
restaurant.put("address2", address2);
restaurant.put("lat", lat);
restaurant.put("lng", lng);
restaurant.put("cost", cost);
restaurant.put("menu_type", menu_type);
restaurant.put("rate", rate);
restaurant.put("offer", offer);
restaurant.put("mobile", mobile);
contactList.add(restaurant);
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"name", "address1",
"address2","lat","lng","menu_type","Phone","rate","offer","cost"}, new int[]{R.id.name,
R.id.address1, R.id.address2,R.id.lat,R.id.lng,R.id.menu,R.id.mobile,R.id.rate,R.id.offer,R.id.cost});
lv.setAdapter(adapter);
}
}
}
答案 0 :(得分:0)
当只有一个结果时,许多API不会返回数组。您将要使用optJSONArray
,如果它返回null,请检查它是否是单个结果(使用其他opt方法),如果是,则跳过数组步骤。
答案 1 :(得分:0)
我建议使用Google Gson库来解析json字符串。
基本上你需要导入库,创建相应的POJO并调用gson.fromJson(jsonStringToParseFrom,YourPOJOClassName.class)。所以你的doInBackground看起来像:
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Gson gson = new Gson();
YourPOJOClass parsedResponse = gson.fromJson(jsonStr, YourPOJOClass.class);
如果在gson中会出现一些解析问题,你会得到明确错误的异常,你将摆脱所有的样板解析代码。