gpt4 book ai didi

java - 基本网络.performRequest : Unexpected response code 422

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:23 26 4
gpt4 key购买 nike

我正在使用 volley 库通过 POST 方法将用户注册到后端服务器。但它在 android logcat 中显示 BasicNetwork.performRequest: Unexpected response code 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);


}

最佳答案

请使用@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表示请求体不正确。

谢谢。

关于java - 基本网络.performRequest : Unexpected response code 422,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43544324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com