【问题标题】:How to resolve Unexpected response code 400 While POST data using API如何在使用 API POST 数据时解决意外的响应代码 400
【发布时间】:2018-12-12 09:07:06
【问题描述】:

由于我无法使用此 api 发布数据,我得到的响应代码为 400,所以你能帮我摆脱这个吗 这是我的活动

String BASE_URL = "http://74.207.233.160/api/v1/users";
StringRequest stringRequest = new StringRequest(Request.Method.POST, BASE_URL,new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        progressBar.setVisibility(View.GONE);
        Log.d("SignupActivity", "onResponse" +response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(SignupActivity.this, "Error.......", Toast.LENGTH_SHORT).show();
        error.printStackTrace();
    }
}){
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("email", email);
        params.put("password", password);
        params.put("role", role);
        return params;
    }
};
MySingleTon.getInstance(SignupActivity.this).addToRequestQueue(stringRequest);

这是我的 SingleTon 类

public class MySingleTon {

private static MySingleTon mInstance;
private RequestQueue requestQueue;
private static Context mCtx;

private MySingleTon(Context context){
    mCtx = context;
    requestQueue = getRequestQueue();
}

public static synchronized MySingleTon getInstance(Context context){
    if (mInstance == null){
        mInstance = new MySingleTon(context);
    }
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (requestQueue == null){
        requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
    }
    return  requestQueue;
}

public void addToRequestQueue(Request request){
    requestQueue.add(request);
}
}

这里是错误的详细信息

E/Volley: [4370] BasicNetwork.performRequest: Unexpected response code 400 for http://74.207.233.160/api/v1/users

这是我的邮递员屏幕截图 postman post data 从上面的邮递员那里,如果我使用用户 [email],我会收到响应代码 500

【问题讨论】:

    标签: java android json string


    【解决方案1】:

    附加的 Postman 屏幕截图使用标题来提供键值对。在您的示例中,您正在覆盖 getParams 方法,并且实际上您正在对 URL 发出 POST 请求:

    http://74.207.233.160/api/v1/users?email=youremail&amp;password=yourpassword&amp;role=yourrole;

    如果我理解正确,您应该覆盖 getHeaders 方法。

    编辑: 根据 cmets,您需要的是请求正文中的 JSON 字符串。要设置适当的请求标头,请使用 JsonBodyRequest 类:

    import com.android.volley.toolbox.JsonObjectRequest;
    import org.json.JSONObject;
    ...
    JSONObject body = new JSONObject();
    body.put("email", "youremail");
    body.put("role", "yourrole");
    
    final String requestBody = body.toString();
    
    String BASE_URL = "http://74.207.233.160/api/v1/users";
    StringRequest stringRequest = new JsonObjectRequest(Request.Method.POST, BASE_URL,new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            progressBar.setVisibility(View.GONE);
            Log.d("SignupActivity", "onResponse" + String.valueOf(response));
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(SignupActivity.this, "Error.......", Toast.LENGTH_SHORT).show();
            error.printStackTrace();
        }
    }){
        @Override
        public byte[] getBody() {
            // Here you provide the body from the previously stringified JSON object
            return requestBody.getBytes("utf-8");
        }
    };
    

    【讨论】:

    • 如果它在体内,那该怎么办
    • 那么你应该覆盖public byte[] getBody() throws AuthFailureError。请咨询documentation
    • 另请参阅MDN 以更好地了解 http POST 请求的内容。 :)
    • 当时需要getparams和getHeaders哈
    • 实际上在邮递员中只是它的正文,但在错误地截屏时我点击了标题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多