我正在使用volley库通过POST方法将用户注册到后端服务器。但它显示了BasicNetwork.performRequest:android logcat中的意外响应代码422。任何帮助?它也是将用户的数据上传到我的服务器。但是没有给予恰当的回应/结果。
还尝试添加或删除
params.put("Content-Type", "application/json");
params.put("Accept", "application/json");
和
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
这是我的代码
private void register() {
final ProgressDialog mDialog = new ProgressDialog(RegisterActivity.this);
final String email = "" + inputEmail.getText().toString().trim();
final String password = "" + inputPassword.getText().toString().trim();
final String address = "" + inputAddress.getText().toString().trim();
final String phone = "" + inputPhone.getText().toString().trim();
final String gender = "" + spnGender.getSelectedItem().toString().trim();
final String bloodgroup = "" + spnBloodGroup.getSelectedItem().toString().trim();
final String name = "" + inputName.getText().toString().trim();
final String role = "user";
final String status = "true";
mDialog.setTitle("Authenticating..");
mDialog.setMessage("Signing in...");
mDialog.setCancelable(false);
mDialog.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, REGISTER_URL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("RESULT", response.toString());
mDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("RESULT", "Error: " + error.getMessage());
// hide the progress dialog
mDialog.hide();
Toast.makeText(RegisterActivity.this, error.toString(), Toast.LENGTH_LONG).show();
Log.d("ERROR RESPONSE", "result= " + error);
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
Toast.makeText(RegisterActivity.this, "Connection timeout, please try after sometime..", Toast.LENGTH_LONG).show();
} else if (error instanceof AuthFailureError) {
//TODO
Toast.makeText(RegisterActivity.this, "Authentication Failed", Toast.LENGTH_LONG).show();
} else if (error instanceof ServerError) {
//TODO
Toast.makeText(RegisterActivity.this, "Server Error", Toast.LENGTH_LONG).show();
} else if (error instanceof NetworkError) {
//TODO
Toast.makeText(RegisterActivity.this, "Network Problem", Toast.LENGTH_LONG).show();
} else {
if (error instanceof ParseError) {
//TODO
Toast.makeText(RegisterActivity.this, "Server Busy", Toast.LENGTH_LONG).show();
}
}
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
return headers;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("Accept", "application/json");
params.put("phone", phone);
params.put("address", address);
params.put("gender", gender);
params.put("bloodgroup", bloodgroup);
params.put("role", role);
params.put("username", name);
params.put("password", password);
params.put("email", email);
params.put("status", status);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(RegisterActivity.this);
requestQueue.add(jsonObjReq);
}
答案 0 :(得分:0)
请使用@Override
更新您的代码protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("phone", phone);
params.put("address", address);
params.put("gender", gender);
params.put("bloodgroup", bloodgroup);
params.put("role", role);
params.put("username", name);
params.put("password", password);
params.put("email", email);
params.put("status", status);
return params;
}
422表示请求正文不正确。
感谢。