为什么 Volley String 请求不能在 x-www-form-urlencoded 中使用 POST 方法发送 Body 参数?

why can't Volley String request send Body parameters with POST method in x-www-form-urlencoded?

首先,我应该说我在这里读了几个类似的故事,但是其中 none 解决了我收到 400 错误代码的问题。 我想使用 Post 方法向 android studio 3.3.1 和 volley 库中的服务器发送一些登录信息。我在 getBody 中将参数作为键值对发送,并在 getBodyContentType() 中设置内容类型。 服务器仅获取 xxx-form-urlencoded 格式的参数。我使用 Postman 作为代理来查看正在发送的内容... 邮递员以原始格式接收 Body 中的参数,而不是 xxx-form-urlencoded,我认为这就是服务器无法获取登录参数并且我收到 400 错误代码的原因(BasicNetwork.performRequest:意外响应代码 400)- 顺便说一句,服务器是用 asp.net 和 IIS 网络服务器编写的。

 private  void PostParams()
        {
             Boolean is_valid_user;
            try {

                String URL = "http://192.168.43.22:5555/AOBServices/login";
                JSONObject jsonBody = new JSONObject();
                jsonBody.put("my_very_special_key","Love");
                jsonBody.put("username", "amir");
                jsonBody.put("password", "1234");
                jsonBody.put("grant_type", "password");
                final String requestBody = jsonBody.toString();
                final StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Log.i("VOLLEY", response);
                        Toast.makeText(LoginActivity.this, "SUCCESSSSSSSSSSSSS \n response: "+response, Toast.LENGTH_SHORT).show();
                        //update shared prefs.....
                        myResponse=response;
                        shprefs= new sharedPrefsClass(getApplicationContext());
                        shprefs.setDefaults("USER_ID", mEmail);
                        shprefs.setDefaults("IS_SIGNEDIN", true);
                        startActivity(new Intent(LoginActivity.this,MainActivity.class));
                        finish();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        // As of f605da3 the following should work
                        NetworkResponse response = error.networkResponse;
                        if (error instanceof ServerError && response != null) {
                            try {
                                String res = new String(response.data,
                                        HttpHeaderParser.parseCharset(response.headers, "utf-8"));

                            } catch (UnsupportedEncodingException e1) {
                                // Couldn't properly decode data to string
                                e1.printStackTrace();
                            }

                        }
                    }


                }) {
                    @Override
                    public String getBodyContentType() {
                        return "application/x-www-form-urlencoded; charset=UTF-8";

                    }
                    @Override
                    protected String getParamsEncoding() {
                        return "utf-8";
                    }


                    @Override
                    public byte[] getBody() throws AuthFailureError {
                        try {
                                 return requestBody == null ? null : requestBody.getBytes("utf-8");

                        } catch (UnsupportedEncodingException uee) {
                            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                                 return null;
                        }
                    }

                 @Override
                    protected Response<String> parseNetworkResponse(NetworkResponse response) {
                        String responseString = "";
                        if (response != null) {
                            responseString = String.valueOf(response.statusCode);
                            // can get more details such as response.headers
                        }
                        return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
                    }
                };

                requestQueue.add(stringRequest);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

对于那些面临类似问题的人: 在对 Volley 感到失望之后,我通过 HttpUrlConnection 实现了网络功能,并发现了问题所在: 我以 json 对象格式发送参数,即 {key:value,...} 而服务器期望 key:value&key:value&... 所以返回到 Volley 并像上面那样制作参数字符串并在 getBody() 中使用它,解决了我的问题。 :)