这是获取群组的方法:
public void getGroupsThree() {
String tag_json_obj = "json_obj_obj"; /* tag used to cancel the request */
String loginUrl = "removed for security reasons"; /* login rest url */
/* show the progress bar */
PD = new ProgressDialog(getActivity());
PD.setMessage("Loading...");
PD.show();
/* this are the params to post */
Map<String, String> params = new HashMap<String, String>();
params.put("userID", "13");
params.put("secretKey", "andrew");
params.put("starts", "5");
params.put("limits", "10");
CustomRequestJsonArray jsonArr;
jsonArr = new CustomRequestJsonArray(Request.Method.POST, loginUrl, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i = 0; i < response.length(); i++) {
try {
JSONObject groupData = (JSONObject) response.get(i);
Toast.makeText(getActivity(), "Response: " + groupData.getString("groupID"), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
Toast.makeText(getActivity(), "Json Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
PD.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
/* handle request error */
Toast.makeText(getActivity(), "Response " + error.getMessage(), Toast.LENGTH_LONG).show();
PD.hide();
}
}, params);
/* add the request to the request queue */
VolleyApplication.getInstance().addToRequestQueue(jsonArr, tag_json_obj);
}
这是我的CustomRequestJsonArray类
public class CustomRequestJsonArray extends JsonRequest<JSONArray> {
private Response.Listener<JSONArray> listener;
private Map<String, String> params;
public CustomRequestJsonArray(int method, String url, String requestBody,
Response.Listener<JSONArray> listener, Response.ErrorListener errorListener,Map<String, String> params) {
super(method, url, requestBody, listener,
errorListener);
this.params = params;
}
@Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
return params;
};
@Override
protected void deliverResponse(JSONArray response) {
listener.onResponse(response);
}
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
响应错误
“JSONObject无法转换为JSONArray”
我认为没有发布参数。这是我希望的:
[
{
"groupID": "28",
"userID": "13",
"name": "ACS Test Edit",
"description": "Edited by Joe",
"createdOn": "2015-09-29 08:55:49",
"active": "1"
},
{
"groupID": "25",
"userID": "13",
"name": "ICT consulting",
"description": "",
"createdOn": "2015-09-28 07:32:33",
"active": "1"
}
]
如果出现任何错误,例如无效的安全密钥或缺少参数,我应该这样:
{
"code": "404",
"error": "You are not permitted to access this resource"
}
当我运行应用程序时,这就是我要得到的:
引起:org.json.JSONException:值{“error”:“您不允许访问此资源”,org.json.JSONObject类型的“code”:“404”}无法转换为JSONArray < / p>
所以,我这个参数没有发布。我如何确保他们被张贴?
答案 0 :(得分:0)
因为成功时来自服务器应用的响应是JSONArray
,如果错误是JSONObject
,并且您想要使用JsonArrayRequest
,那么我建议您使用parseNetworkResponse
看起来如下:
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, "utf-8"));
// Check if it is JSONObject or JSONArray
Object json = new JSONTokener(jsonString).nextValue();
JSONArray jsonArray = new JSONArray();
if (json instanceof JSONObject) {
jsonArray.put(json);
} else if (json instanceof JSONArray) {
jsonArray = (JSONArray) json;
}
return Response.success(jsonArray,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException e) {
return Response.error(new ParseError(e));
}
}
关于错误消息You are not permitted to access this resource
,您可能错过了请求标头中的凭据。如果邮件包含您的凭据,则应在邮递员中检查您的请求。