0

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" :""}]

我收到此错误:

休息响应错误:: 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","org.json.JSONArray 类型的 product_cost":"","product_off":""}] 无法转换为 JSONObject

我的安卓代码:

_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);}

你能帮我修一下吗?

4

4 回答 4

1

你的回应是JSONArray,但你正在执行JsonObjectRequest,所以你Response.Listener是期待的JSONObject。这就是为什么凌空不能JSONArray投到JSONObject. 使用JsonArrayRequest。所以你Response.Listener会期望JSONArray

于 2018-06-06T06:16:36.137 回答
1

试试这个它会帮助你

在这里,您需要从响应中获取 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();
        }
    }

请让我知道这对你有没有用。或者如果它对您有用,请将其标记为有用。

于 2018-06-06T06:21:10.443 回答
0

试试这个

productList = new Gson().fromJson(response.toString(),new TypeToken<ArrayList<Category>>() {
    }.getType());
于 2018-06-06T06:13:04.050 回答
0

该错误来自这样一个事实,即您期望响应一个 JSONArray 作为您的 HTTP 请求的结果,但是您在 OnResponse 侦听器中创建了一个具有 JSOnObject 参数的 JsonObjectRequest

你要做的是:

  • 做一个 JsonArrayRequest

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

这个链接非常有用,因为它提供了一个: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);}
于 2021-04-29T19:25:07.370 回答