使用齐射将数据发布到服务器时出现错误代码400

时间:2018-01-09 08:14:28

标签: android json android-edittext android-volley

我正在使用将数据发布到服务器。我正在使用 fileds接受来自用户的数据。我只接受first_name,last_name,用户名,电子邮件,电话,地址。格式数据就像

{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "user": {
                "first_name": "Satyam",
                "last_name": "Gondhale",
                "username": "satyam@gmail.com",
                "email": "satyam@gmail.com",
                "groups": [],
                "is_active": true
            },
            "phone": "9028571487",
            "address": "Pune"
        }
    ]
}  

Post请求就像

 private void sendData() {

      String req="request";
      String url="http://192.168.1.106:9500/api/userprofile/";
      JSONObject jsonObject=new JSONObject();
      try
      {
          jsonObject.put("count","");
          jsonObject.put("next","");
          jsonObject.put("previous","");
          JSONArray jsonArray=new JSONArray();
          JSONObject jsonObjectUser=new JSONObject();
          jsonObjectUser.put("first_name",first_name);
          jsonObjectUser.put("last_name",last_name);
          jsonObjectUser.put("username",username);
          jsonObjectUser.put("email",email);
          JSONObject jsonObject1=new JSONObject();
          jsonObject1.put("user",jsonObjectUser);
          jsonObject1.put("address","Pune");
          jsonObject1.put("phone",phone);
          jsonArray.put(jsonObject1);
          jsonObject.put("results",jsonArray);
      }
      catch (JSONException e)
      {
           e.printStackTrace();
      }

        JsonObjectRequest request=new JsonObjectRequest(Request.Method.POST,url,null,new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Toast.makeText(getActivity(),"Done Success",Toast.LENGTH_SHORT).show();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }


        })
        {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                String credentials = name+":"+pass;
                String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                headers.put("Content-type", "application/json");
                headers.put("Authorization", auth);
                return headers;
            }

        };

        AppController.getInstance().addToRequestQueue(request,req);
    }

}  

当我发送数据时,我收到错误代码400.如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

而不是尝试设置getHeaders()的内容类型,尝试设置为getBodyContentType()

@Override
public Map<String, String> getBodyContentType() throws AuthFailureError {
   Map<String, String> headers = new HashMap<>();
   String credentials = name+":"+pass;
   String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
   headers.put("Content-type", "application/json");
   headers.put("Authorization", auth);
   return headers;
}