简体   繁体   English

齐射JSONObjectRequest Request.Method.POST但收到GET

[英]Volley JSONObjectRequest Request.Method.POST but received GET

I made a Custom JSONObjectRequest as below code. 我做了一个自定义JSONObjectRequest如下代码。

Currently i made the Request.Method.POST and i had confirmed it while passing to the super class here super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener); 目前,我发出了Request.Method.POST并在传递给此处的超类时进行了确认super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener); . It was received as int 1 (POST), but when received on my server (PHP), the log was receiving as "REQUEST_METHOD":"GET" 它以int 1 (POST)的形式接收,但是在我的服务器(PHP)上接收时,日志的接收形式为"REQUEST_METHOD":"GET"

Have anyone met this kind of problem or can help to point out what i had missed in below code. 有没有人遇到过这种问题,或者可以帮助指出以下代码中我错过的内容。 As you can see below, i had make sure below things: 正如您在下面看到的,我已经确定以下几点:

  • params not empty params不为空
  • there is function getParams() there and the params are not empty 那里有函数getParams()并且params不为空
  • i even put getBody() and make sure it not returning null 我什至放了getBody()并确保它不返回null

Refer below code for what i had tried to do 请参阅以下代码以了解我尝试做的事情

try {
    JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jsonObject) {

            if (jsonObject != null && jsonObject.length() > 0) {
                try {
                    if (jsonObject.getInt("status") == ResponseCode.LOGIN_FAILED) {

                        SharedManager sharedManager = new SharedManager();
                        sharedManager.logoutUser(context, true);

                        return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            callback.onSuccessResponse(jsonObject);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            callback.onErrorResponse(error);

        }
    }) {
        /**
         * Passing some request headers
         * */
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return headers;
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            try {
                return JSONHelperConverter.jsonToMapString(params);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8;";
        }

        @Override
        public byte[] getBody() {
            try {
                String postBody = null;
                try {
                    postBody = createPostBody(JSONHelperConverter.toMapString(params));
                    postBody = params.toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return postBody == null ? null : postBody.getBytes("utf-8");
            } catch (NegativeArraySizeException n) {
                n.printStackTrace();
                return null;
            } catch (UnsupportedEncodingException uee) {

                uee.printStackTrace();
                return null;
            }
        }

        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            String responseString;
            JSONObject jsonObject = null;

            if (response != null) {
                try {
                    responseString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));

                    jsonObject = new JSONObject(responseString);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
        }
    };

    NetworkSingleton.getInstance(context).addToRequestQueue(stringRequest);
} catch (NegativeArraySizeException n) {
    n.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

I solved my own problem Sent POST request but server says GET request in Android volley, what I am doing wrong here? 我解决了自己的问题发送了POST请求,但是服务器在Android Volley中说GET请求,我在这里做错了什么?

Solution: Due to i'm requesting to PHP server, i need to point out the filename. 解决方案:由于我正在请求PHP服务器,因此我需要指出文件名。

Currently i point the request to a URL without Filename extension, and i just add index.php to my end of URL and it worked. 目前,我将请求指向不带文件扩展名的URL,我只是将index.php添加到URL的末尾,它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Volley JsonObjectRequest发布请求忽略参数 - Volley JsonObjectRequest Post request ignoring params URL请求在android Request.Method.POST中不起作用? - URL Request not working in android Request.Method.POST? 凌空:JsonObjectRequest中的onResponse延迟请求 - Volley: onResponse in JsonObjectRequest delayed request “org.json.JSONObject”缺少“Request.Method.GET”和“com.android.volley”JsonObjectRequest arguments 出现故障 - “org.json.JSONObject” missing “Request.Method.GET” and “com.android.volley” JsonObjectRequest arguments appear to out of order VOLLEY / ANDROID:发布请求但未收到参数 - VOLLEY / ANDROID : post request but parameters not received 如何在android volley中使用jSONObjectRequest发出PATCH请求? - How to make a PATCH request with jSONObjectRequest in android volley? JsonObjectRequest GET 方法返回值后请求获取值 - JsonObjectRequest GET Request retrieves value after method returns value Volley:创建一个处理 JSONArrayRequest 和 JSONObjectRequest 的方法 - Volley: Create a method that takes care of both JSONArrayRequest and JSONObjectRequest 如何在Volley库中使用POST和GET方法? - How to use POST and GET method in Volley library? 带有JSONObjectParameter的Volley GET JsonObjectRequest根本不起作用 - Volley GET JsonObjectRequest with JSONObjectParameter doesn't work at all
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM