【问题标题】:How to resolve Unexpected response code 400 While POST data using API with Volley Library如何使用带有 Volley 库的 API POST 数据时解决意外的响应代码 400
【发布时间】:2018-12-12 11:46:40
【问题描述】:

因为我无法使用此 API 发布数据。我得到了 400 的响应代码,所以你能帮我摆脱这个吗。

这是我的活动

String BASE_URL = "http://74.207.233.160/api/v1/users";
try {
                    JSONObject body = new JSONObject();
                    body.put("email", email);
                    body.put("password", password);
                    body.put("role", role);

                    final String requestBody = body.toString();

                    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" + 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() throws AuthFailureError {

                            try {
                                return requestBody.getBytes("utf-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                                return null;
                            }
                        }
                    };

                    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);
}
}

这里是错误的详细信息

12-12 17:00:56.206 13835-14349/oakridgebs.com.flexijob I/System.out: [socket][/10.162.143.245:37838] 已连接 [OkHttp] 发送请求>> 12-12 17:00:56.207 13835-14349/oakridgebs.com.flexijob I/System.out: [OkHttp] sendRequesthttp://74.207.233.160/api/v1/users 的意外响应代码 400 12-12 17:00:57.123 13835-13835/oakridgebs.com.flexijob W/System.err: com.android.volley.ClientError 在 com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:199) 12-12 17:00:57.123 13835-13835/oakridgebs.com.flexijob W/System.err: 在 com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131) 在 com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111) 在 com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90) 12-12 17:00:57.134 13835-13835/oakridgebs.com.flexijob D/ViewRootImpl:启用硬件加速,this = ViewRoot{a22a09 Toast,ident = 2} 12-12 17:00:57.177 13835-13876/oakridgebs.com.flexijob D/mali_winsys: new_window_surface 返回 0x3000 12-12 17:01:12.371 13835-13876/oakridgebs.com.flexijob W/MALI: glDrawArrays:770: [MALI] glDrawArrays 在这里花费超过 5 毫秒。总经过时间(我们):6950 12-12 17:01:54.101 13835-13876/oakridgebs.com.flexijob W/MALI: glDrawArrays:770: [MALI] glDrawArrays 在这里花费超过 5 毫秒。总经过时间(我们):8209 12-12 17:01:59.890 13835-13835/oakridgebs.com.flexijob I/InputMethodManager: [startInputInner] EditorInfo { packageName=oakridgebs.com.flexijob, inputType=0x81, imeOptions=0x4001006, privateImeOptions=null }, windowGainingFocus=null , mServedView=android.support.v7.widget.AppCompatEditText{290b673e VFED..CL .F...... 40,344-660,432 #7f0800c1 app:id/signup_confirm_password}, mServedInputConnectionWrapper=android.view.inputmethod.InputMethodManager$ControlledInputConnectionWrapper@ 36ef7c3c 12-12 17:01:59.915 13835-13835/oakridgebs.com.flexijob D/ActivityThread: ACT-AM_ON_PAUSE_CALLED ActivityRecord{5272e92 token=android.os.BinderProxy@38cb8063 {oakridgebs.com.flexijob/oakridgebs.com.flexijob.SignupActivity }} 12-12 17:01:59.934 13835-13835/oakridgebs.com.flexijob D/ActivityThread: 处理的 ACT-PAUSE_ACTIVITY : 0 / android.os.BinderProxy@38cb8063

我无法发布数据,因为我在运行代码时显示响应 400。 请问可以解决吗?

【问题讨论】:

标签: javascript java android api android-volley


【解决方案1】:

您熟悉 HTTP 请求的结构吗?不完整的请求通常会返回 400 错误。当我缺少 Host 字段时,我通常会在使用 curl 或 Telnet 发出请求时遇到错误。确保在您的起始行之后,有一些类似于 Host:example.com 的内容。这是 HTTP 1.1 的要求。

如果您一直遇到错误,我会进行 Wireshark 数据包捕获并查看 HTTP 数据包的样子。这可以帮助您确定请求中是否缺少 Host 字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    相关资源
    最近更新 更多