简体   繁体   中英

store json response in array list

error:value passed: ("contact") not found. given code is to get json format data in array list.i'm doing this task in android studio. when i run the below code, i could see the json format data with error that "contact" not found.i have change that with obj and other declared variable,but still facing same problem. any suggestion to resolve this?
here is the code:

 private String TAG = MainActivity.class.getSimpleName();

private ProgressDialog pDialog;
private ListView lv;

// URL to get contacts JSON
private static String url = "http://services.groupkt.com/state/get/IND/all";

List<JsonBean> listobj = new ArrayList<>();

//  ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  //lv = (ListView) findViewById(R.id.list);

    new GetContacts().execute();
}

private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        Httphanlder sh = new Httphanlder();

        // 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 contacts = jsonObj.getJSONArray("contacts");

               JsonBean jsonbeanobj=new JsonBean();

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

                    jsonbeanobj.setCountry(c.getString("country"));
                    jsonbeanobj.setName(c.getString("name"));
                    jsonbeanobj.setAbbr(c.getString("abbr"));
                    jsonbeanobj.setArea(c.getString("area"));
                    jsonbeanobj.setCapital(c.getString("capital"));

                    listobj.add(jsonbeanobj);
                  }
            } 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();

    }

}

This might not be an answer to that problem, but usually when I see people using JSONObject..etc they are new to programming. I used to do the same :)

Have a look at Gson or Jackson Library, it will deserialise the json to a java object for you.

Then in that example you can do this using Jackson:

List contacts = Array.asList(mapper.readValue(json, Contact[].class));

public class Contact {
    @JsonCreator
    public Contact(@JsonProperty("name") name...etc) 
      this.name = name; 
    }
}

You need to use json array "result"

update this line

// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("result");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM