使用Volley

时间:2016-07-30 08:22:42

标签: android http-post http-status-code-404 android-volley

我已经尝试了stackoverflow上可用的每一种方法,但结果保持不变。

  

我想使用volley在头文件的Authorization字段中发送params和api key。

以下是代码段。

 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:http://www.XXXXXXXXXXX.com/comments的意外响应代码400

我已经使用Advanced Rest客户端测试了端点,它可以工作并返回带有之前添加的注释的JsonObject。

结果如下:

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

1 个答案:

答案 0 :(得分:0)

好的,经过长时间的研究和愚蠢的相当有价值的改变后,它终于为我工作了。

以下是工作代码:

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

注意:它对我有用,我不确定其他人。

更改是:将JsonObjectRequest更改为StringRequest 删除任何内容类型并没有任何区别,因此我将其删除。

  

另外,确保所有参数都有一些值,即通过记录测试它。