0

I want to use Volley in my code which Request type should I use for my response below I can't understand. URL::: http://localhost/api/product/read.php

    {
      "data":[
        {
         "category_id":"1",
         "category_name":"Today's Recipe",
         "recipes":[
          {
           "id":"1",
           "recipe_name":"Peanut, and Chilli Noodles",
           "ingredients":"Serves: 4 \r\n\r\n250g (9 oz) fresh Chinese ,
           "prepration":"Prep:15min Cook:10min Ready in:25min \r\n\r\nCook 
           noodles in a large pot of boiling water until done.  have 
           chilli paste, use minced red chilli to taste."
     }
    ]
  },
nimi0112
  • 2,065
  • 1
  • 18
  • 32
Chitra Nandpal
  • 413
  • 4
  • 24
  • this appears to be a JSON Object, so you should use a JsonObjectRequest https://developer.android.com/training/volley/request.html – joao86 Dec 27 '17 at 12:25
  • you should use JsonRequest. Refer to my answer on this blog : https://stackoverflow.com/questions/47936955/i-am-unable-to-make-a-network-call-using-volley-i-have-tried-many-solutions-but/47937120#47937120 – Chandan Bhandari Dec 27 '17 at 12:29
  • when i use jsonobject request following error display::: com.android.volley.ParseError: org.json.JSONException: Value Connect of type java.lang.String cannot be converted to JSONObject – Chitra Nandpal Dec 27 '17 at 12:30
  • 1
    I see a comma at the end is this typo or it is really an incomplete array? – Enzokie Dec 27 '17 at 12:38
  • I doubt that the json data is not proper. The `data` array is not closed, i guess `"prepration"` is part of `ingredients` property. And fyi, your localhost url will only work for you and not us as the server is deployed in your system. – SripadRaj Dec 27 '17 at 12:44

2 Answers2

1

Try this:

RequestQueue requestQueue = Volley.newRequestQueue(context);
    final String url = "url here";

    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response)
                {
                Log.e(" result",(String.valueOf(response)));
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    );
   requestQueue.add(getRequest);

Hope this helps.

nimi0112
  • 2,065
  • 1
  • 18
  • 32
0
StringRequest jsonObjectRequest = new StringRequest(Request.Method.POST, url,  new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                if (response != null) {
                    try {
JSONObject jsonObject = new JSONObject(response);
                        if (jsonObject.getString("status").equalsIgnoreCase("success")) {
                    JSONArray jsonArray = jsonObject.getJSONArray("data"); 
if(jsonArray!=null && jsonArray.length()>0){
    for(int i=0;i<jsonArray.length();i++){
JSONObject data_object=jsonArray.getJSONObject(i);
JSONArray recepie = data_object.getJSONArray("recipes"); 

}
//                           
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                }

        });
        MySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest);
        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
                APIConstants.API_TIMEOUT, //for 30 Seconds
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    }

Hope this helps

Enzokie
  • 7,365
  • 6
  • 33
  • 39
Mohit Hooda
  • 273
  • 3
  • 9