0

下面是我的 json 代码,但它只从 json 文件返回单个值我从 json 文件中只得到一个星期五,但实际上在 json 文件中有 4 个星期五我如何获得星期五的所有值?当我运行我的代码时ListView只显示这个星期五的值“dish_name”:“Hamburger”,我如何获得星期五的所有四个值

  {
      "status": 1,
      "data": [{
          "school_name": "LittleStar ",
          "calendar_id": "1",
          "menu_title": "Atypical Menu",
          "dish_name": "Mandarin Chicken Rice Bowl",
          "dish_id": "210",
          "day": "Fri",
          "Schedule": "weekly"
      }, {
          "school_name": "LittleStar ",
          "calendar_id": "1",
          "menu_title": "Atypical Menu",
          "dish_name": "Chicken Burger",
          "dish_id": "206",
          "day": "Fri",
          "Schedule": "weekly"
      }, {
          "school_name": "LittleStar ",
          "calendar_id": "1",
          "menu_title": "Atypical Menu",
          "dish_name": "Garden Fresh Salad",
          "dish_id": "211",
          "day": "Fri",
          "Schedule": "weekly"
      },


      {
          "school_name": "LittleStar ",
          "calendar_id": "1",
          "menu_title": "Atypical Menu",
          "dish_name": "A Test",
          "dish_id": "213",
          "day": "Mon",
          "Schedule": "weekly"
      }, {
          "school_name": "LittleStar ",
          "calendar_id": "1",
          "menu_title": "Atypical Menu",
          "dish_name": "Hamburger",
          "dish_id": "56",
          "day": "Fri",
          "Schedule": "weekly"
      }, {
          "school_name": "LittleStar ",
          "calendar_id": "1",
          "menu_title": "Atypical Menu",
          "dish_name": "Tofu Caesar Salad w\/ Roll",
          "dish_id": "149",
          "day": "Fri",
          "Schedule": "weekly"
      }]
  }

HelloworldActivity.java

  public class HelloworldActivity extends Activity {
int count = 0;
Date date1;
Date date2;
String id;
String title;
String byweekdate;
int IOConnect = 0;
String mVal2;
String mVal3;
String mVal9;
String SelectMenuAPI;
String url;
String URL;
String URL2, URL3, URL4;
String menu_title;
JSONArray school;
ListView listCategory;
String status;
String message;
String _response;
String SelectMenuAPI2;
TextView menu_nametxt;
thirdstepAdapter cla;
static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> school_name = new ArrayList<String>();
static ArrayList<String> menu_name = new ArrayList<String>();

static ArrayList<String> dish_name = new ArrayList<String>();
static ArrayList<String> dish_ID = new ArrayList<String>();
static ArrayList<String> day = new ArrayList<String>();

static ArrayList<Long> Vacation_ID = new ArrayList<Long>();
static ArrayList<String> Vacation_name = new ArrayList<String>();
static ArrayList<String> Vacation_Date = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    listCategory = (ListView) findViewById(R.id.thirdscreenlist);
    menu_nametxt = (TextView) findViewById(R.id.menu_name);
    cla = new thirdstepAdapter(HelloworldActivity.this);
    listCategory.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
        }
    });

    new getDataTask().execute();
}

void clearData() {
    Category_ID.clear();
    school_name.clear();
    menu_name.clear();
    dish_name.clear();
    dish_ID.clear();
    day.clear();
    Vacation_ID.clear();
    Vacation_name.clear();
    Vacation_Date.clear();
}

public class getDataTask extends AsyncTask<Void, Void, Void> {
    getDataTask() {
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        parseJSONData();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub

        if ((Category_ID.size() > 0) || IOConnect == 0) {

            listCategory.setAdapter(cla);
            menu_nametxt.setText(mVal2);
            // cla.notifyDataSetChanged();
        }
    }
}

public void parseJSONData() {
    clearData();
    SelectMenuAPI = "http://198.57.208.46/~school/index.php/api/index/getMenuBySchool?sid=147&lid=1";
    URL = SelectMenuAPI;
    URL2 = URL.replace(" ", "%20");

    try {
        Log.i("url", "" + URL2);
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams
                .setConnectionTimeout(client.getParams(), 15000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(URL2);
        HttpResponse response = client.execute(request);

        HttpEntity resEntity = response.getEntity();
        _response = EntityUtils.toString(resEntity);

        JSONObject json2 = new JSONObject(_response);

        status = json2.getString("status");
        if (status.equals("1")) {

            JSONArray school = json2.getJSONArray("data");

            for (int i = 0; i < school.length(); i++) {
                JSONObject object = school.getJSONObject(i);
                if (count == 1) {
                    count = 0;
                    break;
                } else

                if (object.getString("Schedule").equals("weekly")) {
                    if (object.getString("day").equals("Fri")) {
                        Category_ID.add((long) i);
                        school_name.add(object.getString("school_name"));
                        dish_ID.add(object.getString("dish_id"));
                        dish_name.add(object.getString("dish_name"));
                        menu_name.add(object.getString("menu_title"));
                        day.add(object.getString("day"));
                        count = count + 1;
                        String[] mVal = new String[school.length()];
                        for (int k = 0; k < school.length(); k++) {
                            mVal[k] = school.getJSONObject(k).getString(
                                    "menu_title");
                            message = "weekly";
                            mVal2 = mVal[0];
                            // count = count + 1;
                        }

                    } else {
                    }
                } else if (object.getString("Schedule").equals("biweekly")) {

                    if (object.getString("day").equals(byweekdate)) {
                        Category_ID.add((long) i);
                        school_name.add(object.getString("school_name"));
                        menu_name.add(object.getString("menu_title"));
                        day.add(object.getString("day"));
                        String[] mVal = new String[school.length()];
                        for (int k = 0; k < school.length(); k++) {
                            mVal[k] = school.getJSONObject(k).getString(
                                    "menu_title");

                        }
                        message = "weekly";
                        mVal2 = mVal[0];

                        dish_name.add(object.getString("dish_name"));

                        count = count + 1;

                    } else {
                    }
                } else {
                }
            }
        } else {
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        IOConnect = 1;
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}
4

1 回答 1

0
Try like this hope this help u

     try {
    JSONObject jb = new JSONObject(myjsonstring);
    String totalitems = jb.getString("totalItems");
    Log.i("......", "" + totalitems);
    String totalpages = jb.getString("totalPages");
    String itemsPerPage = jb.getString("itemsPerPage");
    String currentPage = jb.getString("currentPage");
    JSONArray jarr = jb.getJSONArray("items");
    for (int i = 0; i < jarr.length(); i++) {
        JSONObject jb1 = jarr.getJSONObject(i);
        String id = jb1.getString("id");
        String categoy = jb1.getString("category");
        String title = jb1.getString("title");
        String pic = jb1.getString("thumbnail");
        Log.i("........", id);

    }
} catch (Exception e) {

}


//HTTP CLASS

 public static String sendRequest(String url) {
        String result = "";
        try {
            HttpClient client = new DefaultHttpClient();
            HttpParams httpParameters = client.getParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
            HttpConnectionParams.setSoTimeout(httpParameters, 5000);
            HttpConnectionParams.setTcpNoDelay(httpParameters, true);
            HttpGet request = new HttpGet();
            request.setURI(new URI(url));
            HttpResponse response = client.execute(request);
            InputStream ips = response.getEntity().getContent();
            BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
            StringBuilder sb = new StringBuilder();
            String s;
            while (true) {
                s = buf.readLine();
                if (s == null || s.length() == 0)
                    break;
                sb.append(s);
            }
            buf.close();
            ips.close();
            result = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
于 2013-10-08T07:46:34.967 回答