在数组列表中存储json响应

时间:2017-05-18 12:57:16

标签: java json android-studio arraylist

错误:传递的值:("联系")未找到。 给定的代码是在数组list.i中获取json格式数据,并在android studio中执行此任务。当我运行下面的代码时,我可以看到json格式数据,错误是"联系" not found.i已经改变了obj和其他声明的变量,但仍面临同样的问题。 有什么建议可以解决这个问题吗? 这是代码:

 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();

    }

}

2 个答案:

答案 0 :(得分:0)

这可能不是这个问题的答案,但通常当我看到人们使用JSONObject..etc时,他们不熟悉编程。我以前做过同样的事情:)

看看Gson或Jackson Library,它会将json反序列化为java对象。

然后在那个例子中你可以用杰克逊做到这一点:

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

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

答案 1 :(得分:0)

你需要使用json数组“result”

更新此行

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