如何使用API​​解决意外响应代码400 While POST数据

时间:2018-12-12 09:07:06

标签: java android json string

由于我无法使用此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 从上面的邮递员那里,如果我使用user [email],我将得到响应代码500

1 个答案:

答案 0 :(得分:0)

附加的Postman屏幕快照正在使用标头提供键值对。在您的示例中,您将覆盖getParams方法,并且实际上是对URL进行POST请求:

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

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

编辑: 根据注释,您需要的是请求正文中的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");
    }
};