使用排球提出请求

时间:2015-01-29 08:28:58

标签: android android-volley

我正在尝试使用PUT方法更新服务器上的数据,但是我收到了这个错误,我以前没有使用过凌空,我有什么遗漏吗?请建议。

我的代码:

private void sendRequest() {
        RequestQueue queue = Volley.newRequestQueue(getActivity());

        PersonalInfoModel.Data.PersonalInformation.Address  address;
        address = new  PersonalInfoModel().new Data().new PersonalInformation().new Address();
        address.setBuilding("test");
        address.setCountry("test");
        address.setCounty("test");
        address.setPostcodeInCode("");
        address.setPostcodeOutCode("");
        address.setTown("");

        PersonalInfoModel.Data.PersonalInformation  data;
        data = new  PersonalInfoModel().new Data().new PersonalInformation();
        data.setDob("tsst");
        data.setEmail("testemai");
        data.setForename("test");
        data.setSurname("data");
        data.setAddress(address);

        Gson gson = new Gson();
        JSONString = gson.toJson(data);
        try {
            obj = new JSONObject(JSONString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        LogUtils.LOGD(TAG, "JSONString  is :: " + JSONString.toString());
        JsonObjectRequest putRequest = new JsonObjectRequest(Request.Method.PUT, urlUserDetail, obj,
                new Response.Listener<JSONObject>()
                {
                    @Override
                    public void onResponse(JSONObject response) {
                        // response
                        LogUtils.LOGD("Response", response.toString());
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // error
                        LogUtils.LOGD("Error.Response", error.toString());
                    }
                }
        ) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("tess", "2222");
                params.put("Authorization", "def1bc98d032");
                params.put("Content-Type", "application/json; charset=utf-8");

                return params;
            }

            @Override
            public byte[] getBody() {

                try {
                    LogUtils.LOGD("json", obj.toString());
                    return obj.toString().getBytes("UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return null;
            }
        };

        queue.add(putRequest);


    }

错误:

BasicNetwork.performRequest: Unexpected response code 400 for url

5 个答案:

答案 0 :(得分:3)

@Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("tess", "2222");
                params.put("Authorization", "def1bc98d032");

              // add this parameter
               params.put("Content-Type", "application/json; charset=utf-8");
                return params;
            }

答案 1 :(得分:2)

  1. 检查PostMan或某个客户端,检查您是否收到了正确的回复并消除了这种可能性 - 请求无需使用Android排球即可使用
  2. 响应代码400表示错误请求。这意味着您提出的请求不正确。可能内容类型有错误,请尝试添加

    params.put("Content-Type", "application/json; charset=utf-8");getHeaders()

答案 2 :(得分:1)

尝试删除getbody方法..因为您将请求参数作为json对象发送,内容类型标题也是application / json.so getbody将不是必需的

答案 3 :(得分:0)

JsonObjectRequest默认添加Content-Type: application/json; charset=utf-8。您的params.put("Content-Type", "application/json; charset=utf-8");会添加另一个Content-Type,而某些应用程序无法使用多个Content-Type定义。您应该尝试删除params.put("Content-Type", "application/json; charset=utf-8");

答案 4 :(得分:0)

确保PUT请求的请求URL具有资源的id作为最后一个路径段。否则你会得到404。

Wrong form of URL:

/users

Correct form of URL:

/users/bob