获取''org.json.JSONArray cannot be converted to JSONObject''
Getting ''org.json.JSONArray cannot be converted to JSONObject''
我正在尝试使用 volley 库 post json 方法在我的 android 应用程序中以 json 格式从我的服务器获取数据。但是每次都会得到一个 'org.json.JSONArray cannot be converted to JSONObject'.
这是错误:
Error: org.json.JSONException: Value [{"status":"Success","code":1313,"msg":"Request completed successfully"}] of type org.json.JSONArray cannot be converted to JSONObject.
这就是我的代码:
RequestQueue requestQueue = Volley.newRequestQueue(this);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("auth", "---------");
jsonObject.put("request", "Login");
jsonObject.put("Name", "raky");
jsonObject.put("Email", "exp@a.com");
} catch (JSONException e) {
e.printStackTrace();
}
String url = "http://my.website_name.com/";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("VOLLEY", response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR.VOLLEY", error.getMessage());
}
});
jsonObjectRequest.setTag(1);
requestQueue.add(jsonObjectRequest);
您的回复是一个数组,
您可以使用一些 api 测试工具(例如 Postman)来测试您的 api 并查看您从 api 中得到了什么,您也可以使用 StringRequest 而不是 JsonObjectRequest,通过这种方法您可以获得任何类型的响应并转换为您需要的类型
这里的问题是您的网站响应正文对象是一个 JSONArray
[
{
"status": "Success",
"code": 1313,
"msg": "Request completed successfully"
}
]
所以你会得到异常,因为在响应处理程序中你想要一个 JSONObject
而你不能将 JSONArray
转换为 JSONObject
.
你的服务器(网站)必须 return 给你的是 root JSONObject
然后在它的节点树中它可能有 JSONArray
,但根必须是 JSONObject
.
因此 修复您的服务器端代码 以便 returns:
{
"status": "Success",
"code": 1313,
"msg": "Request completed successfully"
}
我正在尝试使用 volley 库 post json 方法在我的 android 应用程序中以 json 格式从我的服务器获取数据。但是每次都会得到一个 'org.json.JSONArray cannot be converted to JSONObject'.
这是错误:
Error: org.json.JSONException: Value [{"status":"Success","code":1313,"msg":"Request completed successfully"}] of type org.json.JSONArray cannot be converted to JSONObject.
这就是我的代码:
RequestQueue requestQueue = Volley.newRequestQueue(this);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("auth", "---------");
jsonObject.put("request", "Login");
jsonObject.put("Name", "raky");
jsonObject.put("Email", "exp@a.com");
} catch (JSONException e) {
e.printStackTrace();
}
String url = "http://my.website_name.com/";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("VOLLEY", response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR.VOLLEY", error.getMessage());
}
});
jsonObjectRequest.setTag(1);
requestQueue.add(jsonObjectRequest);
您的回复是一个数组, 您可以使用一些 api 测试工具(例如 Postman)来测试您的 api 并查看您从 api 中得到了什么,您也可以使用 StringRequest 而不是 JsonObjectRequest,通过这种方法您可以获得任何类型的响应并转换为您需要的类型
这里的问题是您的网站响应正文对象是一个 JSONArray
[
{
"status": "Success",
"code": 1313,
"msg": "Request completed successfully"
}
]
所以你会得到异常,因为在响应处理程序中你想要一个 JSONObject
而你不能将 JSONArray
转换为 JSONObject
.
你的服务器(网站)必须 return 给你的是 root JSONObject
然后在它的节点树中它可能有 JSONArray
,但根必须是 JSONObject
.
因此 修复您的服务器端代码 以便 returns:
{
"status": "Success",
"code": 1313,
"msg": "Request completed successfully"
}