json数组格式为
[{"restaurants": {
"items1": [{
"id": 4,
"item_name": "Special Pizza",
"image": ""
}, {
"id": 5,
"item_name": "Veg Cheese Pizza",
"image": ""
}]
"items2": [{
"id": 4,
"item_name": "Special Pizza",
"image": ""
}, {
"id": 5,
"item_name": "Veg Cheese Pizza",
"image": ""
}]
}}
我想获得item1& item2将值作为Expandable ListView
中的父项传递,并将item_name传递给child。如何获取父名称并设置为setter方法?
class AsyncT1 extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// http://abservetech.com/demoprojects/android/food_app/public/mobile/restaurant/restaurantresults
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://abservetechdemo.com/projects/android/food_app/public/mobile/restaurant/resdetails");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("res_id","1"));
nameValuePairs.add(new BasicNameValuePair("status","veg"));
nameValuePairs.add(new BasicNameValuePair("main_cat","pizza"));
// nameValuePairs.add(new BasicNameValuePair("group_id", group_id));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());
/* execute */
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
InputStream inputStream = response.getEntity().getContent();
InputStreamToStringExample str = new InputStreamToStringExample();
String responseServer = str.getStringFromInputStream(inputStream);
Log.d("response", "response -----" + responseServer);
JSONObject jsonRootObject = new JSONObject(responseServer);
JSONArray jsonArray = jsonRootObject.optJSONArray("restaurants");
for (int i1 = 0; i1 < jsonArray.length(); i1++) {
try {
ArrayList<Group> list = new ArrayList<Group>();
ArrayList<Child> ch_list= new ArrayList<Child>();
JSONObject jsonObject = jsonArray.getJSONObject(i1);
Group gru = new Group();
JSONArray ja = jsonObject.getJSONArray("item");
Log.d("subcategory---",s_category);
gru.setName(jsonObject.getJSONArray("items").toString());
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
Child ch = new Child();
ch.setName(jo.getString("item_name").toString());
ch_list.add(ch);
} // for loop end
gru.setItems(ch_list);
list.add(gru);
ExpAdapter = new ExpandListAdapter(
Expand.this, list);
runOnUiThread(new Runnable() {
@Override
public void run() {
ExpandList.setAdapter(ExpAdapter);}});
}
catch (JSONException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
}
}
Logcat错误:
W/System.err: java.lang.NullPointerException
06-30 12:30:39.260 26691-26744/abservetech.com.foodapp W/System.err: at abservetech.com.foodapp.Expand$AsyncT1.doInBackground(Expand.java:84)
06-30 12:30:39.260 26691-26744/abservetech.com.foodapp W/System.err: at abservetech.com.foodapp.Expand$AsyncT1.doInBackground(Expand.java:50)
06-30 12:30:39.260 26691-26744/abservetech.com.foodapp W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-30 12:30:39.260 26691-26744/abservetech.com.foodapp W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-30 12:30:39.260 26691-26744/abservetech.com.foodapp W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-30 12:30:39.260 26691-26744/abservetech.com.foodapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-30 12:30:39.260 26691-26744/abservetech.com.foodapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-30 12:30:39.260 26691-26744/abservetech.com.foodapp W/System.err: at java.lang.Thread.run(Thread.java:841)
答案 0 :(得分:0)
第一件餐馆是json对象而不是json数组。让你的小组像这样做
JSONObject jsonRootObject = new JSONObject(responseServer);
JSONObject jsonobject=jsonRootObject.getJSONObject("restaurants");
JSONArray item1=jsonobject.getJSONArray("items1");
JSONArray item2=jsonobject.getJSONArray("items2");
for(int i=0;i<item1.length();i++)
{
JSONObject json=item1.getJSONArray(i);
String item_name=json.getString("item_name");
}