1

JSON data format: I am trying to fetch this array json data but not getting it.

[{"Items":"Chicken Burger","Price":"250","Quantity":"2"}, 
{"Items":"Hamburger","Price":"230","Quantity":"3"}]

MyCartActivity class: I tried using Volley and I am now getting that JSONArray cannot be converted to JSONObject error. Can you mention where to change so that I can get my data?

 final JsonObjectRequest request = new JsonObjectRequest (Request.Method.POST, fetchurl, 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 object = jsonArray.getJSONObject(i);

                    String Price = object.getString("Price");
                    String Name = object.getString("Items");
                    String Quantity = object.getString("Quantity");

                    adapter = new CartAdapter(MyCartActivity.this, list);
                    list.add(new CartPojo(Price, Name, Quantity));
                    adapter.notifyDataSetChanged();

                    progressbar.setVisibility(View.GONE);
                }

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

     requestQueue.add(request);
  }

Adapter class:

    public CartAdapter(Activity activity, List datalist) {

    this.activity = activity;
    this.datalist = datalist;
   }

   @Override
   public int getCount() {
   return datalist.size();
  }

  @Override
  public Object getItem(int position) {
    return datalist.get(position);
 }

  @Override
  public long getItemId(int position) {
    return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null)
        convertView = inflater.inflate(R.layout.cart_item_layout, null);

    TextView ItemName = convertView.findViewById(R.id.item_name);
    TextView Price = convertView.findViewById(R.id.item_price);
    TextView Quantity = convertView.findViewById(R.id.items_quantity);

    CartPojo pojo = datalist.get(position);

    ItemName.setText(String.valueOf(pojo.getName()));
    Price.setText(String.valueOf(pojo.getPrice()));
    Quantity.setText(String.valueOf(pojo.getQuantity()));

    return convertView;
  }

Please provide me some solutions....

1 Answer 1

0

The response can't be parsed into JSONObject, because its not that, but a JSONArray. You can send a simple StringRequest and then parse the response string into an JSONArray using JSONArray(response) constructor.

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        JSONArray arr = new JSONArray(response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

Or use the JsonArrayRequest. Here's an example.

Sign up to request clarification or add additional context in comments.

14 Comments

Thank you @Andrej I'll check out using this
I have changed JsonObjectRequest to StringRequest as you mentioned about it but still no changes for the process of fetching data. It is showing Frames are Skipped... what can be the reason behind this....
Do I have to make some changes over Adpater class also? please let me know where should I have to make those changes....
Frames are skipped because you are doing too much processing on the main thread. Take a look at threading in Android. Do you get a response with a stringRequest?
No I didn't get any response
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.