我正在使用volley JsonObjectRequest,我获得了一些json格式的json响应,并获得了一些格式的400响应代码。
Json格式
{
"errors": "This password is too short. It must contain at least 8 characters & Please provide a valid id type",
"message": "Account could not be created with received data.",
"status": "Bad request"
}
凌空调用
public void userRegistraion(String identifier, String password, String idType){
final String URL = "https://someweb.com/auth/user/registration/";
//Post params to be sent to the server
JSONObject params = new JSONObject();
try {
params.put("identifier", identifier);
params.put("password", password);
params.put("id_type", idType);
}catch (JSONException e){
e.printStackTrace();
}
JsonObjectRequest request_json = new JsonObjectRequest(Request.Method.POST,URL,params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("volley response",response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("volley", "Error: " + error
+ "\nStatus Code " + error.networkResponse.statusCode
+ "\nResponse Data " + error.networkResponse.data
+ "\nResponse header " + error.networkResponse.headers
+ "\nCause " + error.getCause()
+ "\nmessage" + error.getMessage());
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
return headers;
}
@Override
public String getBodyContentType() {
return "application/json";
}
};
// add the request object to the queue to be executed
Myapplication.getInstance().addToRequestQueue(request_json);
}
错误:
E/Volley: [74123] BasicNetwork.performRequest: Unexpected response code 400 for https://someweb.com/auth/user/registration/
但是对于具有相同请求的此json类型,不会发生错误。
{"user":{"th_user_id":"KSJDYDYD",
"is_active":true,
"is_verified":false,
"role":"user",
"created_at":"2018-03-21T06:25:02.556490Z",
"updated_at":"2018-03-1T06:25:02.556516Z",
"verified_at":null,"last_login":"2018-03-21T06:25:02.570251Z",
"documents":[]},
"token":"11f2faea30847db4u3hfhu0384hh392dac46"}
答案 0 :(得分:0)
400错误请求错误是一个HTTP状态代码,表示您发送到服务器中的api端点的JsonObjectRequest请求不正确或已损坏,服务器无法理解。
我建议你让你的php文件报告所有这样的错误,以便你获得有关此错误的更多信息
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);