将参数传递到Android Volley POST请求,返回JSON

时间:2015-03-29 20:21:45

标签: android json android-volley

我正在尝试传递Volley POST请求,该请求传递参数friends_phone_number_csv,该参数应该返回一个JSON对象。但是,在使用下面的请求时,它只是注意到:

 E/Volley﹕ [4230] BasicNetwork.performRequest: Unexpected response code 500 for http://(ip-address):3000/getActivatedFriends.json

在chrommes POSTMAN中测试此请求我知道webservice是正确的,应该返回一个JSON对象。

我该如何做到这一点?

app中的POST请求:

JsonObjectRequest getUserActiveFriends = new JsonObjectRequest(Request.Method.POST, "http://" + Global.getFeastOnline() + "/getActivatedFriends.json",
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        // Parse the JSON:
                        try {
                            resultObject = response.getJSONObject("friends_match");

                            Toast.makeText(getApplicationContext(), resultObject.toString(), Toast.LENGTH_LONG).show();
                            // PARSE THE REST

                            //Log.v("USER ID", "The user id is " + userId);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // error
                        Log.d("Error.Response", error.toString());
                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("friends_phone_number_csv", contactsNumbers);

                return params;
            }
        };
        requestQueue.add(getUserActiveFriends);

1 个答案:

答案 0 :(得分:0)

您应该添加此代码,当您发布请求并返回json数据时,您应该添加内容类型&#34; application / json;字符集= UTF-8&#34;到http标题。

 @Override
 public Map<String, String> getHeaders() throws AuthFailureError {
   Map<String, String> headers = new HashMap<String, String>();                   
   headers.put("Content-Type", "application/json; charset=utf-8");
   return headers;
 }