使用Volly使用Recyclerview实现JSON对象

时间:2017-12-23 03:06:26

标签: android json web-services android-fragments geojson

我无法使用recylerview获取此Json对象数据。

  

{ “0”:{ “ID”: “1”, “图像”: “acar.jpg”, “名称”: “马鲁蒂”, “等级”: “45”}, “1”:{” ID “:” 2" , “图像”: “bcar.jpg”, “名称”: “BMW”, “等级”: “55”}, “2”:{ “ID”: “3”, “图像” : “ccar.jpg”, “名称”: “迅速”, “等级”: “46”}, “3”:{ “ID”: “4”, “图像”: “dcar.jpeg”, “姓名” : “喷粉”, “等级”: “56”}, “4”:{ “ID”: “5”, “图像”: “ec​​ar.jpg”, “名称”: “奥迪”, “等级”:” 58 “},” 成功“:1}

Fragment.class

 View view = inflater.inflate(R.layout.fragment_fragment_first, container, false);
    final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycleview);
    RecyclerView.LayoutManager lm = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(lm);
    recyclerView.setHasFixedSize(true);
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());

    StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject=new JSONObject(response);
                Log.e(TAG,response);
                for(int i=0;i<jsonObject.length();i++){
                    DataModel dataModel=new DataModel();
                    dataModel.setName(jsonObject.getString("name"));
                    dataModel.setRating(jsonObject.getString("rating"));
                    data.add(dataModel);
                    adapter=new CustomAdapter(data,getContext());
                }
                recyclerView.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getContext(),
                        "Error: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    requestQueue.add(stringRequest);


    // Inflate the layout for this fragment
    return view;
}

我的数据模型类

public class DataModel {
private String images;
private String name;
private String rating;
public DataModel(String images, String name, String rating) {
    this.images = images;
    this.name = name;
    this.rating = rating;
}
public  DataModel(){
}
public String getImages() {
    return images;
}

public void setImages(String images) {
    this.images = images;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getRating() {
    return rating;
}

public void setRating(String rating) {
    this.rating = rating;
}

2 个答案:

答案 0 :(得分:2)

你的解析器错了。并且您正在错误的位置创建适配器(您永远不会在循环内创建适配器)。使用下面的代码,。

 try {
        JSONObject resObject = new JSONObject(jsonString);
        Iterator<String> iterator = resObject.keys();
        while (iterator.hasNext()) {
            JSONObject inner = resObject.getJSONArray(iterator.next());
            DataModel dataModel=new DataModel();
            dataModel.setName(inner.getString("name"));
            dataModel.setRating(inner.getString("rating"));
            data.add(dataModel);
        }
        adapter=new CustomAdapter(data,getContext());
        recyclerView.setAdapter(adapter);
    }catch (Exception e){
        e.printStackTrace();
    }

答案 1 :(得分:0)

试试这个,

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ArrayList<DataModel> dataList=new ArrayList<DataModel>();
    View view = inflater.inflate(R.layout.fragment_fragment_first, container, false);

        RequestQueue rq = Volley.newRequestQueue(getActivity());
        String url = "...Your URL.....";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                 try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    Iterator<String> iterator = jsonObject.keys();
                    JSONArray jsonArray = new JSONArray();
                    while (iterator.hasNext()) {
                        String key = iterator.next();
                        jsonArray.put(jsonObject.get(key));
                    }

                    for (int i = 0; i < jsonArray.length()-1; i++) {
                      String Image=jsonArray.getJSONObject(i).getString("images");
                        String Name = jsonArray.getJSONObject(i).getString("name");
                        String Rating= jsonArray.getJSONObject(i).getString("rating");
                        Log.d("LOG","IMAGE :"+Image+"\tNAME"+Name+"\tRATING ="+Rating);

        DataModel dataModel=new DataModel();
        dataModel.setName(Name);
        dataModel.setRating(Rating);
        dataList.add(dataModel);
        }
adapter=new CustomAdapter(dataList,getActivity());
recyclerView.setAdapter(adapter);
                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("RESPONSE", "Error [" + error + "]");
            }
        });
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(90000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        rq.add(stringRequest);
    }