如何使用齐射库从数组中获取json数据?

时间:2018-02-07 19:27:46

标签: android json listview android-studio android-volley

我的json数据

{"code":1200,"message":"Data Retrieved","data":[{"id":1,"name":"Vangipurapu Venkata Sai Laxman","skills":"Cricketer, Batsman","image":"https:\/\/qph.ec.quoracdn.net\/main-qimg-4f5029c4319b41270f5643d461979645-c"},{"id":2,"name":"Himesh Reshammiya","skills":"music director, singer, producer, lyricist, distributor and actor","image":"https:\/\/starsunfolded-1ygkv60km.netdna-ssl.com\/wp-content\/uploads\/2016\/01\/Himesh-Reshammiya-nasal-singing.jpg"},{"id":3,"name":"Rajkummar Rao","skills":"Indian actor","image":"https:\/\/encrypted-tbn0.gstatic.com\/images?q=tbn:ANd9GcQhShfz5g33MOXBKtLlEXo16uuxEpHFL8NYQE2lg071avavYeKr"},{"id":4,"name":"Pusarla Venkata Sindhu","skills":"badminton player","image":"https:\/\/encrypted-tbn0.gstatic.com\/images?q=tbn:ANd9GcTC0wYTxk72MNx5IADgDDMAqUz9AEyfR6UZexWNqn_fKFNZCLz-"}]}

这是我的主动

MainActivity.java

JsonArrayRequest rqst = new JsonArrayRequest(url,
         new Response.Listener<JSONArray>() {
             @Override
             public void onResponse(JSONArray response) {   
                  for (int i = 0; i < response.length(); i++) {
                      try {    
                          JSONObject obj = response.getJSONObject(i);
                          DataSet dataSet = new DataSet();
                          dataSet.setName(obj.getString("name"));
                          dataSet.setImage(obj.getString("image"));                                 
                          dataSet.setSkills(obj.getString("skills"));
                          list.add(dataSet);
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }    
                  }
                  adapter.notifyDataSetChanged();
              }

我应该做些什么改变才能获取数据?我应该将这些数据提取到列表视图中。

1 个答案:

答案 0 :(得分:0)

您期待JSONArray,但您的响应是包含JSON数组的JSON对象。

将您的请求修改为StringRequest

    StringRequest strtRequest = new StringRequest(Request.Method.POST, url, 
        new Response.Listener<String>() 
        {
            @Override
            public void onResponse(String response) {
               JSONObject obj = new JSONObject(response);
                   JSONArray arr=obj.getJSONArray("data");
                   for(int i=0;i<arr.length();i++){
                          JSONObject jo=arr.getJSONObject(i);
                          DataSet dataSet = new DataSet();
                          dataSet.setName(jo.getString("name"));
                          dataSet.setImage(jo.getString("image"));                                 
                          dataSet.setSkills(jo.getString("skills"));
                          list.add(dataSet);
            }
 }
        }, 
        new Response.ErrorListener() 
        {
             @Override
             public void onErrorResponse(VolleyError error) {
                             // error
                 Log.d("Error.Response", response);
           }
        }
    ) ;