Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中使用volley通过post-param发出JSONArray请求_Java_Android_Android Volley - Fatal编程技术网

Java 如何在Android中使用volley通过post-param发出JSONArray请求

Java 如何在Android中使用volley通过post-param发出JSONArray请求,java,android,android-volley,Java,Android,Android Volley,这是获取组的方法: 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

这是获取组的方法:

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);

    }
如果出现任何错误,如安全密钥无效或参数丢失,我应该得到以下信息:

{
  "code": "404",
  "error": "You are not permitted to access this resource"
}
当我运行应用程序时,我得到的是:

原因:org.json.JSONException:Value{“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));
    }
}
@覆盖
受保护的响应parseNetworkResponse(NetworkResponse响应){
试一试{
String jsonString=新字符串(response.data,
HttpHeaderParser.parseCharset(response.headers,“utf-8”);
//检查它是JSONObject还是JSONArray
Object json=new-JSONTokener(jsonString.nextValue();
JSONArray JSONArray=新的JSONArray();
if(JSONObject的json实例){
jsonArray.put(json);
}else if(JSONArray的json实例){
jsonArray=(jsonArray)json;
}
返回Response.success(jsonArray,
HttpHeaderParser.parseCacheHeaders(响应));
}捕获(不支持的编码异常e){
返回Response.error(新的ParseError(e));
}捕获(JSONException e){
返回Response.error(新的ParseError(e));
}
}

关于错误消息
,不允许您访问此资源
,可能您在请求的标题中丢失了凭据。您应该在Postman中检查您的请求是否包含包含您的凭据的标题。

我认为您被错误弄糊涂了。您发布的第一个JSON响应是JSONArray,而错误响应是JSONObject(第一个响应中有[])。在您的响应中,您总是解析引起问题的JSONArray。不管怎样,你有什么问题?错误的解析或身份验证错误?我不知道您试图如何在服务器端处理登录凭据,但从我看到的情况来看,服务器不接受它。因此,身份验证错误。正如@Rohan Kandwal所说,您应该检查您的服务器端。除此之外,我建议您直接使用
JSONArrayRequest
,这等于您的自定义
CustomRequestJsonArray
,如果您想查看请求中是否存在post参数,你可以使用诸如Charles之类的工具来捕获你的数据包。我已经尝试过直接使用JSONArrayRequest,但我不知道如何将它与POST方法结合使用。当我使用postman来测试其余的数据包时,它起作用了
{
  "code": "404",
  "error": "You are not permitted to access this resource"
}
@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));
    }
}