使用 Volley 发送 POST 参数和 headers 时代码响应代码 400

Code response code 400 when sending POST params and headers using Volley

我已经尝试了 Whosebug 上可用的每一种方法,但结果仍然相同。

I want to send params and also api key in the Authorization field of header using volley.

这是代码片段。

 holder.comment_button.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           String POSTCOMMENT_URL = Utility.getIPADDRESS()+"comments";
           volleySingleton = VolleySingleton.getInstance();
           requestQueue = volleySingleton.getRequestQueue();
           JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, POSTCOMMENT_URL, null, new Response.Listener<JSONObject>() {
               @Override
               public void onResponse(JSONObject response) {
                    Log.e("Response:  " , response.toString());
                   Toast.makeText(myApplication.getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
               }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   Toast.makeText(myApplication.getApplicationContext(),"Error", Toast.LENGTH_LONG).show();
               }
           }){
               @Override
               public String getBodyContentType() {
                   return "application/x-www-form-urlencoded; charset=UTF-8";
               }
               @Override
               public Map<String, String> getHeaders() throws AuthFailureError {
                   Map<String, String>  params = new HashMap<>();
                   params.put("Authorization", getApiKey() );
                   return params;
               }

               @Override
               protected Map<String, String> getParams() {
                   Map<String, String>  params = new HashMap<>();
                   params.put("pid", "39");
                   params.put("comment", "mobile testing");
                   return params;
               }

           };
           requestQueue.add(jsonObjectRequest);
       }
   });

我收到这个错误:

[5333] BasicNetwork.performRequest: Unexpected response code 400 for http://www.XXXXXXXXXXX.com/comments

我已经使用 Advanced Rest 客户端测试了端点,它可以正常工作,returns JsonObject 之前添加了注释。

结果如下:

{
"error": false
"message": "comment added successfully added!"
"comment_id": "12"
"user_id": "12"
"pid": "39"
"comment": "testing for Whosebug"
"date of comment": "2016-07-30 13:49:08"
}

好的,经过长时间的研究和愚蠢但有价值的更改,它终于对我有用了。

这是工作代码:

holder.comment_button.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(final View view) {

           final String api_key = getApiKey();
           String POSTCOMMENT_URL = Utility.getIPADDRESS()+"comments";
           volleySingleton = VolleySingleton.getInstance();
           requestQueue = volleySingleton.getRequestQueue();
           StringRequest stringRequest = new StringRequest(Request.Method.POST, POSTCOMMENT_URL, new Response.Listener<String>() {
               @Override
               public void onResponse(String response) {
                    Toast.makeText(myApplication.getApplicationContext(),response,Toast.LENGTH_LONG).show();
               }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   Toast.makeText(myApplication.getApplicationContext(),error.toString(),Toast.LENGTH_LONG).show();
               }
           }) {

               @Override
               public Map<String, String> getHeaders() throws AuthFailureError {
                   Map<String, String>  params = new HashMap<>();
                   params.put("Authorization", api_key);
                   return params;
               }

              @Override
               protected Map<String, String> getParams() {
                   Map<String, String>  params = new HashMap<>();
                   params.put("pid", "40");
                   params.put("comment", "testing");
                   return params;
               }

           };
           requestQueue.add(stringRequest);
       }
   });

Note: It worked for me, I'm not sure about others.

更改是:将 JsonObjectRequest 更改为 StringRequest 并且删除任何 Content-Type 没有任何区别,所以我删除了它。

Also, make sure all the params have some values i.e test it by logging.