jsondata:
[{ “PRODUCT_ID”: “2”, “PRODUCT_TITLE”: “测试”, “product_thumbnail”: “”, “PRODUCT_DESCRIPTION”: “测试”, “product_place”: “”, “product_user”: “1” “product_store”: “”, “product_date”: “1546875653”, “product_cost”: “”, “product_off”: “”},{ “PRODUCT_ID”: “3”, “PRODUCT_TITLE”: “测试1”,“product_thumbnail “:””, “PRODUCT_DESCRIPTION”: “测试1”, “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 “:” 测试”, “product_place”: “”, “product_user”: “1”, “product_store”: “”, “product_date”: “1546875653”, “product_cost”: “”, “product_off”: “”} ,{ “PRODUCT_ID”: “3”, “PRODUCT_TITLE”: “测试1”, “product_thumbnail”: “”, “PRODUCT_DESCRIPTION”: “测试1”, “product_place”: “”, “product_user”: “1”,“product_store “:”“,”product_date“:”1546875653“,”product_cost“:”“,”product_off“:”}}]类型org.json.JSONArray无法转换为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);}
你能帮我解决一下吗?
答案 0 :(得分:0)
试试这个
productList = new Gson().fromJson(response.toString(),new TypeToken<ArrayList<Category>>() {
}.getType());
答案 1 :(得分:0)
您的回复是JSONArray
,但您正在执行JsonObjectRequest
,因此您的Response.Listener
也期待JSONObject
。这就是凌空无法将JSONArray
投射到JSONObject
的原因。使用JsonArrayRequest。这样您的Response.Listener
就会期待JSONArray
答案 2 :(得分:0)
试试这个会帮到你
在这里,你需要从你的回复中获取你的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();
}
}
让我知道它是否适合您。或者如果它对你有用,则标记它有用。
答案 3 :(得分:0)
错误来自这样一个事实,即您期望响应 JSONArray 作为您的 HTTP 请求的结果,但您创建了一个 JsonObjectRequest,它在 OnResponse 侦听器中具有一个 JSOnObject 参数
你需要做的是:
因此,您将一个 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);}