【发布时间】:2018-12-22 21:46:16
【问题描述】:
我正在尝试使用 volley 库 post json 方法在我的 android 应用程序中以 json 格式从我的服务器获取数据。但是每次得到一个'org.json.JSONArray 不能转换为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);
【问题讨论】:
-
从您的网站返回了什么?您能否也发布原始响应正文?
-
错误添加,请检查
-
它返回包裹在方括号
[{your json body}]中的 JSON。删除它们:{your json body}或用括号括起来。重要的是,您的根主体 JSON 主体元素是 JSONObject 而不是 JSONArray。
标签: android json android-volley