如何使用带有Volley Library的API解决意外的响应代码400 While POST数据

时间:2018-12-12 11:46:40

标签: javascript java android api android-volley

由于我无法使用此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] sendRequest >>       12-12 17:00:56.207 13835-14349 / oakridgebs.com.flexijob I / System.out:[OkHttp] sendRequest <<       12-12 17:00:57.096 13835-14349 / oakridgebs.com.flexijob E / Volley:[2174] BasicNetwork.performRequest:http://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:at 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:启用了硬件加速,此= 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。 你能解决吗?

Here is my Postman

1 个答案:

答案 0 :(得分:0)

您是否熟悉HTTP请求的结构?通常,未完成的请求会返回400错误。当我缺少主机字段时,我通常会遇到使用curl或Telnet发出请求的错误。确保在开始行之后, Host:example.com 中包含一些内容。这是HTTP 1.1的要求。

如果仍然遇到错误,我将进行Wireshark数据包捕获,并查看HTTP数据包的外观。这可以帮助您确定请求中是否缺少主机字段。