BasicNetwork.performRequest - 意外的响应代码400(POST)

时间:2017-03-16 07:00:44

标签: android android-volley

当我尝试使用Volley StringRequest或JsonObjectRequest通过rest api获取数据时。我总是得到400错误。它与邮递员一起工作正常。 Http请求方法是POST,Content-Type是application / x-www-form-urlencoded。我也检查了我的参数拼写,它看起来很好。我已经尝试过StringRequest和JsonObjectRequest,结果是E / Volley:[10283] BasicNetwork.performRequest:意外的响应代码400.下面是我的代码。我附上了截图。

enter image description here

这是新截图 Error in code

  <div ng-app='myApp'>
    <div ng-controller='ctrl1'>
      <div ng-repeat="rule in rules">
        <md-chips ng-model="data.selectedHeaders">
          <md-chip-template>
            {{$chip}}
            <!-- for testing-->{{rule}}
          </md-chip-template>
        </md-chips>
      </div>
    </div>
  </div>

我已经尝试使用volley JSONObjectRequest

StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_LOGIN, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d(TAG, response.toString());
                    viewHelper.hideDialog();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, error.toString());
                    viewHelper.hideDialog();
                }
            }){
                @Override
                protected Map<String, String> getParams() {
                    // Posting parameters to login url
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("grant_type", "password");
                    params.put("username", email);
                    params.put("password", password);
                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("Content-Type", "application/x-www-form-urlencoded");
                    return params;
                }

                @Override
                public String getBodyContentType(){
                    return "application/x-www-form-urlencoded";
                }
            };

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

            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(stringRequest, "Authenticating User");

任何人都可以帮我解决这个问题。

3 个答案:

答案 0 :(得分:2)

以下是使用Volley库的POST请求的工作示例。
将其添加到build.gradle(模块:app) - 编译&#39; com.mcxiaoke.volley:library:1.0.19&#39;

user

如果有人遇到上述代码问题,请发表评论。
感谢所有试图帮助我的人。

答案 1 :(得分:0)

Unexpected response code 400表示Bad Reuest。

此响应是由使用URL发送错误参数引起的。

要解决:

  1. 使用拼写检查每个参数。

  2. 确保在stringobject中获得回复。 stringRequestJSONObjectReuest

  3. 同时检查Content-type

  4. 将其用作Content-type

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

    JsonObjectReuest使用:

     HashMap<String, String> params = new HashMap<String, String>();
    params.put("name", "BLah");
    
    JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
    new Response.Listener<JSONObject>() {
       @Override
       public void onResponse(JSONObject response) {
           try {
               Log.d("Response:", response.toString());
           } catch (JSONException e) {
               e.printStackTrace();
           }
       }
    }, new Response.ErrorListener() {
       @Override
       public void onErrorResponse(VolleyError error) {
           Log.d("Error: ", error.getMessage());
       }
    });
    

答案 2 :(得分:0)

400意味着您发送到服务器的数据不符合规则的错误请求。请检查服务器所需的参数或这些参数的拼写。 所以请再次检查,希望它对您有用:)