我想在RecycleView中使用volley显示产品

时间:2017-09-28 06:02:06

标签: android arrays json

我尝试了很多在线代码。这是我的JSON对象。请帮助我使用RecycleViewGridLayout中显示此内容。请在下面找到JSON对象。

{
    "total": 3,
    "per_page": 10,
    "current_page": 1,
    "last_page": 1,
    "next_page_url": null,
    "prev_page_url": null,
    "from": 1,
    "to": 3,
    "data": [
        {
            "product_id": 31,
            "image": "http://celebauc.com/app/uploads/funkymobile.jpg",
            "location": "mumbai",
            "price": "400.0000",
            "status": 1,
            "name": "Itarsia",
            "description": "mouse",
            "discount": {}
        },
        {
            "product_id": 13,
            "image": "http://celebauc.com/app/uploads/funkymobile.jpg",
            "location": "Mumbai",
            "price": "100.0000",
            "status": 1,
            "name": "Dynamic Website",
            "description": "d s das asf sdfsdf sdf sdfs fsdfsd f fsd fsdf dsfsdf",
            "discount": {}
        }
    ]
}

2 个答案:

答案 0 :(得分:0)

试试这个

try {
            JSONObject obj = new JSONObject(response);
            String total = obj.getString("total");
            String per_page = obj.getString("per_page");
            // now as per above get all your data like current_page,last_page,

            JSONArray data = obj.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject jsonObject = data.getJSONObject(i);
                String product_id = jsonObject.getString("product_id");// get id from product_id json object
                String image = jsonObject.getString("image");// get id from product_id json object
                String location = jsonObject.getString("location");// get id from product_id json object
                // as per above get all yourJSONArray data 
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

答案 1 :(得分:0)

试试这个

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
            yourEndpointUrl, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                JSONObject object = new JSONObject(response);
                JSONArray dataArray= object.getJSONArray("data");

            for (int i = 0; i < dataArray.length(); i++) {

            JSONObject jsonObject = data.getJSONObject(i);
            String product_id = jsonObject.optString("product_id");
            String image = jsonObject.optString("image");
                }
            }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());

                }
            });