[英]How to make a JSONArray Request with post param using volley in Android
這是獲取組的方法:
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”:“您無權訪問此資源”,“ code”:“ 404”}類型為org.json.JSONObject的對象無法轉換為JSONArray
所以,我這個參數沒有被發布。 如何確定它們已發布?
因為成功時服務器應用程序的響應是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
,也許您錯過了請求標頭中的憑據。 如果標頭中包含您的憑據,則應在Postman中檢查您的請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.