Volley Post方法给了我意想不到的响应代码400?

时间:2016-02-17 12:25:06

标签: android json android-volley

这里有新的android开发尝试使用gson lib将json值发布到服务器我已经转换为json但是当我尝试发送到服务器时抛出400个意外的响应代码在这里让我发布我的代码:

listobj = account_sf_db.toServer();
    Gson gson = new Gson();
    RequestQueue queue = Volley.newRequestQueue(getBaseContext());
 final String yog = gson.toJson(listobj);

    String URL = "http://xxx.xx.xx.xxx/xxx/CRM/AcoountCreatePageLoad.svc/xxt/xxxt/ " +yog ;




            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            String resp = response.toString();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {


                }
            })

    {

        @Override
        protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("god", yog);


        return params;
    }  @Override
       public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("Content-Type", "application/json");
                    headers.put("Accept","application/json");
                    headers.put("User-agent", "My useragent");
                    return headers;
                }

    };
         queue.add(stringRequest);
            stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

}

2 个答案:

答案 0 :(得分:1)

400状态代码用于Bad Request,这意味着服务器不期望这种类型的请求。

  

由于语法格式错误,服务器无法理解该请求。客户不应该在没有修改的情况下重复请求。

请检查您要添加的标题(内容类型,用户代理等)是否需要。服务器可能会发生一个简单的请求而不是json请求。

还尝试读出onErrorResponse中出现的错误。服务器可能已经响应了您预期的参数和请求类型,还讨论了Web开发人员开发的API类型。

答案 1 :(得分:0)

/ xxt / xxxt /

后,您的网址中有空格

String URL =&#34; http://xxx.xx.xx.xxx/xxx/CRM/AcoountCreatePageLoad.svc/xxt/xxxt/&#34; + yog;

删除它,我认为你会没事的。

P.S 400通常表示Url或参数有问题。

相关问题