Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在使用API和Volley库发布数据时解决意外响应代码400_Javascript_Java_Android_Api_Android Volley - Fatal编程技术网

Javascript 如何在使用API和Volley库发布数据时解决意外响应代码400

Javascript 如何在使用API和Volley库发布数据时解决意外响应代码400,javascript,java,android,api,android-volley,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("passwor

因为我无法使用此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);
以下是错误的详细信息

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]sendRequest您熟悉HTTP请求的结构吗?不完整的请求通常会返回400错误。当我缺少主机字段时,我通常会在使用curl或Telnet进行请求时遇到错误。确保在你的起跑线之后,有类似于主机的东西:example.com。这是HTTP 1.1的要求

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

请参阅此项
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);
}
}