vendredi 23 mars 2018

I cannot fetch the json data in android app from django rest framework

private void jsonParse() {

    String url = "http://127.0.0.1:8000/products/products/";

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("");

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject employee = jsonArray.getJSONObject(i);
                            int id = employee.getInt("id");
                            String title = employee.getString("title");

                            String description = employee.getString("description");
                            String price = employee.getString("price");
                            mTextViewResult.append(title + ", " + String.valueOf(id) + ", "+price +"," + description + "\n\n");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    mQueue.add(request);
}

}

/* Api Root Productsview List

Productsview List GET /products/products/ HTTP 200 OK Allow: GET, POST, HEAD, OPTIONS Content-Type: application/json Vary: Accept

[ { "id": 1, "title": "t-shirt", "description": "this is a good t-shirt", "price": "39.99" }, { "id": 2, "title": "Jeans", "description": "this is a jean", "price": "89.99" } ]`Api Root

Api Root The default basic root view for DefaultRouter

GET /products/ HTTP 200 OK Allow: GET, HEAD, OPTIONS Content-Type: application/json Vary: Accept

{ "products": "http://127.0.0.1:8000/products/products/" }` */

[
{
    "id": 1,
    "title": "t-shirt",
    "description": "this is a good t-shirt",
    "price": "39.99"
},
{
    "id": 2,
    "title": "Jeans",
    "description": "this is a jean",
    "price": "89.99"
}

]




Aucun commentaire:

Enregistrer un commentaire