com.android.volley.ParseError: org.json.JSONException: Value [{}] of type org.json.JSONArray cannot be converted to JSONObject

com.android.volley.ParseError: org.json.JSONException: Value [{}] of type org.json.JSONArray cannot be converted to JSONObject

json数据:

[{"product_id":"2","product_title":"test","product_thumbnail":"","product_description":"test","product_place":"","product_user":"1","product_store":"","product_date":"1546875653","product_cost":"","product_off":""},{"product_id":"3","product_title":"test1","product_thumbnail":"","product_description":"test1","product_place":"","product_user":"1","product_store":"","product_date":"1546875653","product_cost":"","product_off":""}]

我收到这个错误:

Rest response error:: com.android.volley.ParseError: org.json.JSONException: Value [{"product_id":"2","product_title":"test","product_thumbnail":"","product_description":"test","product_place":"","product_user":"1","product_store":"","product_date":"1546875653","product_cost":"","product_off":""},{"product_id":"3","product_title":"test1","product_thumbnail":"","product_description":"test1","product_place":"","product_user":"1","product_store":"","product_date":"1546875653","product_cost":"","product_off":""}] of type org.json.JSONArray cannot be converted to JSONObject

我的android代码:

_httpHandler.addRequest(Request.Method.GET, "product", null, response -> {
        try {
            Log.e("response is:",response.toString());

            for (int i = 0; i < response.length(); i++) {
                JSONObject product = response.getJSONObject(String.valueOf(i));

                productList.add(new Product(
                        product.getString("product_id"),
                        product.getString("product_title"),
                        product.getString("product_thumbnail"),
                        product.getString("product_description"),
                        product.getString("product_place"),
                        product.getString("product_user"),
                        product.getString("product_store"),
                        product.getString("product_date"),
                        product.getString("product_off"),
                        product.getString("product_price")));
            }

            ProductAdapter adapter = new ProductAdapter(MainActivity.this, productList);
            recyclerView.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }, error -> {
        Toast.makeText(MainActivity.this, "Get Product Has Error", Toast.LENGTH_LONG).show();
        Log.e("Rest response error: ", error.toString());
    });

并且 AddRequest 函数是:

public void addRequest(int method, String url, JSONObject jsonRequest,
                       Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
    url = api_url + url;
    Log.e("url is: ", url);
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(method, url, jsonRequest, listener, errorListener){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type","application/x-www-form-urlencoded");
            params.put("Accept", "application/json");
            return params;
        }
    };
    requestQueue.add(jsonObjectRequest);}

你能帮我解决一下吗?

试试这个

productList = new Gson().fromJson(response.toString(),new TypeToken<ArrayList<Category>>() {
    }.getType());

您的响应是 JSONArray,但您正在执行 JsonObjectRequest,因此您的 Response.Listener 也期待 JSONObject。这就是为什么 volley 不能将 JSONArray 转换为 JSONObject 的原因。使用 JsonArrayRequest。这样您的 Response.Listener 就会期望 JSONArray

试试这个对你有帮助

在这里您需要从您的响应中获取您的 JSONArray

像这样

try {
            JSONArray json = new JSONArray(response);

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

                JSONObject product= json.getJSONObject(i);
                Log.e("json 1", product.getString("product_title"));

                productList.add(new Product(
                        product.getString("product_id"),
                        product.getString("product_title"),
                        product.getString("product_thumbnail"),
                        product.getString("product_description"),
                        product.getString("product_place"),
                        product.getString("product_user"),
                        product.getString("product_store"),
                        product.getString("product_date"),
                        product.getString("product_off"),
                        product.getString("product_price")));

            }
        ProductAdapter adapter = new ProductAdapter(MainActivity.this, productList);
        recyclerView.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

让我知道它是否适合您。如果对您有用,或将其标记为有用。

错误是由于您期望 JSONArray 作为 HTTP 请求的响应结果,但您创建了一个 JsonObjectRequest,它在 OnResponse 监听器中有一个 JSOnObject 参数

您需要做的是:

  • 创建 JsonArrayRequest

因此,您将把一个 JSONArray 作为参数传递给 OnResponse 侦听器。

这个 link 非常有用,因为它提供了:android - Volley JSON Array request example

最后,您的 AddRequest 函数应该是这样的:

public void addRequest(int method, String url, JSONArray jsonRequest,
                   Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
url = api_url + url;
Log.e("url is: ", url);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(method, url, jsonRequest, listener, errorListener){
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("Content-Type","application/x-www-form-urlencoded");
        params.put("Accept", "application/json");
        return params;
    }
};
requestQueue.add(jsonArrayRequest);}