Android使用Volley解析JSON数组遇类型转换错误,求解决方法
Fixing "JSONArray cannot be converted to JSONObject" Error in Volley Request
The core issue here is that your server returns a JSONArray as the root response, but your code is using JsonObjectRequest which expects a JSONObject. Let's walk through the fixes step by step:
1. Replace JsonObjectRequest with JsonArrayRequest
Since the response is a top-level JSONArray, switch to JsonArrayRequest instead of JsonObjectRequest. This eliminates the type mismatch error entirely.
Corrected MyCartActivity Code
// Initialize your list first (make sure this is done in onCreate) private List<CartPojo> list = new ArrayList<>(); private CartAdapter adapter; // ... rest of your onCreate code ... final JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST, fetchurl, null, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { try { // Loop directly through the root JSONArray for (int i = 0; i < response.length(); i++) { JSONObject object = response.getJSONObject(i); String price = object.getString("Price"); String name = object.getString("Items"); String quantity = object.getString("Quantity"); // Add item to your list list.add(new CartPojo(price, name, quantity)); } // Initialize adapter AFTER populating the list (or before, then notify) adapter = new CartAdapter(MyCartActivity.this, list); // Don't forget to attach the adapter to your ListView/RecyclerView here // Example: listView.setAdapter(adapter); 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);
2. Improve Adapter Type Safety
Your adapter uses raw List types, which can lead to runtime errors. Update it to use generic List<CartPojo> for better type checking:
Corrected Adapter Code
public class CartAdapter extends BaseAdapter { // Assuming you're extending BaseAdapter private Activity activity; private List<CartPojo> datalist; private LayoutInflater inflater; public CartAdapter(Activity activity, List<CartPojo> datalist) { this.activity = activity; this.datalist = datalist; this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return datalist.size(); } @Override public CartPojo 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 (convertView == null) { convertView = inflater.inflate(R.layout.cart_item_layout, parent, false); } 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(pojo.getName()); price.setText(pojo.getPrice()); quantity.setText(pojo.getQuantity()); return convertView; } }
Key Notes:
- Always match your Volley request type to the root structure of the JSON response (JSONArray vs JSONObject).
- Initialize your list before making the request to avoid null pointer exceptions.
- Move adapter initialization outside the loop (either after populating the list or before the request, then call
notifyDataSetChanged()once after adding all items).
内容的提问来源于stack exchange,提问作者Ayush Katuwal
相关产品推荐
相关产品推荐

