JSON异常,找不到ID

时间:2013-06-06 10:00:55

标签: java android mysql

我正在尝试在我的应用中获取listview输出。数据库能够将数据发送到应用程序,但不知何故数据无法输出。显然,错误是org.json.JSONException:没有值为kidID。有人可以帮帮我吗?提前谢谢。

public class DisplayKidsNames extends ListActivity {

    // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> kidsList; //edited

    // url to get all products list
    private static String url_all_products = "http://192.168.0.104:80/android_connect/get_kids_names.php"; //edited

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_KIDS = "kids"; //edited
    private static final String TAG_KID_ID = "kidID"; //edited
    private static final String TAG_KID_NAME = "kidName"; //edited

    // kids JSONArray
    JSONArray kids = null; //edited

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_kids_names); //edited

     // Hashmap for ListView
     kidsList = new ArrayList<HashMap<String, String>>(); //edited

     // Loading kids names in Background Thread
     new LoadAllKidsNames().execute(); //edited

     // Get listview
     ListView lv = getListView();

     // on selecting single product
     // launching Pet Names Screen
     lv.setOnItemClickListener(new OnItemClickListener() {

         @Override
         public void onItemClick(AdapterView<?> parent, View view,
                 int position, long id) {
             // getting values from selected ListItem
             String kidID = ((TextView) view.findViewById(R.id.kidID)).getText().toString(); //edited

             // Starting new intent
             Intent in = new Intent(getApplicationContext(),
                     DisplayPetNames.class); //edited

             // sending kidID to next activity
             in.putExtra(TAG_KID_ID, kidID); //edited

          // starting new activity and expecting some response back
             startActivityForResult(in, 100);
         }
     });

    }

    // Response from Display Pet Names Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // if result code 100
        if (resultCode == 100) {
            // if result code 100 is received
            // means user edited/deleted product
            // reload this screen again
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }

    }

    /**
     * Background Async Task to Load all kids names by making HTTP Request
     * */
    class LoadAllKidsNames extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(DisplayKidsNames.this);
            pDialog.setMessage("Loading. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting All kids names from url
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

            // Check your log cat for JSON response
            Log.d("All Kids Names: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // kids found
                    // Getting Array of Kids
                    kids = json.getJSONArray(TAG_KIDS); //ll

                 // looping through All kids names
                    for (int i = 0; i < kids.length(); i++) {
                        JSONObject c = kids.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_KID_ID); //oo
                        String name = c.getString(TAG_KID_NAME); //oo

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_KID_ID, id);
                        map.put(TAG_KID_NAME, name);

                     // adding HashList to ArrayList
                        kidsList.add(map);
                    }
                } else {
                    System.out.println("Beach, please.");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            DisplayKidsNames.this, kidsList,
                            R.layout.activity_list_names, new String[] { TAG_KID_ID,
                                    TAG_KID_NAME},
                            new int[] { R.id.kidID, R.id.kidName });
                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }

    }

}

0 个答案:

没有答案