0

同じコード/例を使用して、AndroidでJSONとその解析を学習しています
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

しかし、そのgivenullポインタ例外を実装して実行している間

JSONを取得している間 contacts = json.getJSONArray(TAG_CONTACTS);

  JSONArray contacts = null;
 private static String url = "http://api.androidhive.info/contacts/";


 public void initParsing()
 {
    ListView listView = (ListView) findViewById(R.id.listView_song);
     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
     JSONParser jParser = new JSONParser();
     JSONObject json = jParser.getJSONFromUrl(url);
     try {
            // Getting Array of Contacts
            System.out.println("---getJSON");
            contacts = json.getJSONArray(TAG_CONTACTS);

            // looping through All Contacts
            for(int i = 0; i < contacts.length(); i++){
                JSONObject c = contacts.getJSONObject(i);
                 System.out.println("---times");
                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String email = c.getString(TAG_EMAIL);
                String address = c.getString(TAG_ADDRESS);
                String gender = c.getString(TAG_GENDER);

                // Phone number is agin JSON Object
                JSONObject phone = c.getJSONObject(TAG_PHONE);
                String mobile = phone.getString(TAG_PHONE_MOBILE);
                String home = phone.getString(TAG_PHONE_HOME);
                String office = phone.getString(TAG_PHONE_OFFICE);

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

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_EMAIL, email);
                map.put(TAG_PHONE_MOBILE, mobile);

                // adding HashList to ArrayList
                contactList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

//       ListAdapter adapter = new SimpleAdapter(this, contactList,
//                  R.layout.list_item,
//                  new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] {
//                          R.id.name, R.id.email, R.id.mobile });

       // setListAdapter(adapter);
    //  listView.setAdapter(new CustomArrayAdapter(this, TAG_NAME)); // setting the adapter


 }
4

3 に答える 3

1
public void initParsing()
 {
    ListView listView = (ListView) findViewById(R.id.listView_song);
     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
     JSONParser jParser = new JSONParser();
     JSONObject json = jParser.getJSONFromUrl(url);
     try {
            // Getting Array of Contacts
            System.out.println("---getJSON");
           if(json != null && json.has("contacts")){
            contacts = json.getJSONArray("contacts");

            // looping through All Contacts
            for(int i = 0; i < contacts.length(); i++){
                JSONObject c = contacts.getJSONObject(i);
                 System.out.println("---times");
                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String email = c.getString(TAG_EMAIL);
                String address = c.getString(TAG_ADDRESS);
                String gender = c.getString(TAG_GENDER);

                // Phone number is agin JSON Object
                JSONObject phone = c.getJSONObject(TAG_PHONE);
                String mobile = phone.getString(TAG_PHONE_MOBILE);
                String home = phone.getString(TAG_PHONE_HOME);
                String office = phone.getString(TAG_PHONE_OFFICE);

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

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_EMAIL, email);
                map.put(TAG_PHONE_MOBILE, mobile);

                // adding HashList to ArrayList
                contactList.add(map);
            }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

//       ListAdapter adapter = new SimpleAdapter(this, contactList,
//                  R.layout.list_item,
//                  new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] {
//                          R.id.name, R.id.email, R.id.mobile });

       // setListAdapter(adapter);
    //  listView.setAdapter(new CustomArrayAdapter(this, TAG_NAME)); // setting the adapter


 }
于 2013-01-02T13:42:13.720 に答える
0

json配列名が正しいことを確認してください。

contacts = json.getJSONArray("contacts");
于 2013-01-02T13:37:08.143 に答える
0

これを使用する必要があります:

contacts = json.optJSONArray("array name");

これは、応答に配列がある場合は配列を返し、そうでない場合は。を返しnullます。

于 2013-01-02T13:40:19.677 に答える