Volley 无法从服务器获取错误,但向我显示 volley 错误侦听器,为什么?
Volley cant get error from server but shows me volley error listener why?
我需要帮助解决 Volley 库错误。我已经使用 nodejs api 向服务器发送了一些凭据以进行用户注册。在成功案例中,当我的所有凭据对于创建新用户帐户都是唯一的时,它会向我显示所有正确的所有响应等。但是当我发送一些重复的凭据时,意味着新用户的电子邮件与另一个用户的电子邮件相同,它无法显示我使用检查从服务器得到响应的服务器错误,但它显示我截击错误侦听器错误。
我想在这里显示来自服务器的错误,而不是来自 volley 错误的错误listener.Kindly给我解决方案,我现在卡住了。
代码如下:
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (firstName.getText().toString().isEmpty() || firstName.getText().toString().length() <= 3){
Toast.makeText(getContext(), "First name length is too short", Toast.LENGTH_SHORT).show();
}
else if (lastName.getText().toString().isEmpty() || lastName.getText().toString().length() <= 2){
Toast.makeText(getContext(), "Last name length is too short", Toast.LENGTH_SHORT).show();
}
else if (emailUser.getText().toString().isEmpty() || !emailUser.getText().toString().trim().matches(emailPattern)){
Toast.makeText(getContext(), "Enter valid email address", Toast.LENGTH_SHORT).show();
}
else if (mobileNumber.getText().toString().isEmpty() || mobileNumber.getText().toString().length() <= 8){
Toast.makeText(getContext(), "Enter valid mobile number", Toast.LENGTH_SHORT).show();
}
else if (dobUser.getText().toString().isEmpty()){
Toast.makeText(getContext(), "Select date of birth", Toast.LENGTH_SHORT).show();
}
else if (genderUser.getText().toString().isEmpty()){
Toast.makeText(getContext(), "Select gender", Toast.LENGTH_SHORT).show();
}
else if (passwordUser.getText().toString().isEmpty() || !passwordUser.getText().toString().trim().matches(PASSWORD_PATTERN)){
Toast.makeText(getContext(), "Enter valid password.Password must be 1 special character 1 lower letter and 1 Upper letter and 1 Number digit", Toast.LENGTH_SHORT).show();
}
else {
registerUser();
}
}
});
这是我想要的错误无法正确显示的功能,我也在代码中使用检查:
private void registerUser() {
final ProgressDialog progress = new ProgressDialog(getContext());
progress.setTitle("Register Account");
progress.setMessage("Loading...");
progress.setCancelable(false);
progress.show();
RequestQueue queue = Volley.newRequestQueue(getContext());
final JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("first_name", firstName.getText().toString());
jsonObject.put("last_name",lastName.getText().toString());
jsonObject.put("email", emailUser.getText().toString());
jsonObject.put("password", passwordUser.getText().toString());
jsonObject.put("gender",genderUser.getText().toString());
jsonObject.put("phone_number", mobileNumber.getText().toString());
jsonObject.put("date_of_birth", dobUser.getText().toString());
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String status = response.getString("status");
String message = response.getString("message");
if (status.equals("Success")){
progress.dismiss();
Toast.makeText(getContext(), response.toString(), Toast.LENGTH_SHORT).show();
}
else if (status.equals("Fail")){
JSONArray jsonArray = response.getJSONArray("data");
JSONObject jsonObject1 = jsonArray.getJSONObject(0);
String error = jsonObject1.getString("error");
progress.dismiss();
Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
else {
progress.dismiss();
Toast.makeText(getContext(), "Something went wrong.Please try again", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progress.dismiss();
Toast.makeText(getContext(), "Email or mobile number already used by other account.", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
// RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
// queue.add(request);
queue.add(jsonRequest);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getContext(), "Here is the error", Toast.LENGTH_SHORT).show();
}
}
这是来自服务器的错误 json 响应:
{
"status": "Fail",
"message": "Invalid Request",
"data": [
{
"error": "Email already used by another account!"
}
]
}
这是我的 onErrorResponse 方法中的行
String result = Global.parseError(error.networkResponse.statusCode, error.networkResponse.data);
这是我的 parseError 方法中的内容。
public static String parseError(int responseCode, byte[] data) {
StringBuilder stringBuilder = new StringBuilder();
if (data == null)
return null;
else {
if (responseCode == 400 || responseCode == 401 || responseCode == 402 || responseCode == 403 || responseCode == 404) {
String string = new String(data);
Log.d(TAG, "parseError: "+string);
try {
JSONObject jsonObject = new JSONObject(string);
Log.d(TAG, "parseError: " + jsonObject);
if (jsonObject.has("message") && !jsonObject.isNull("message"))
stringBuilder.append(jsonObject.getString("message"));
if (jsonObject.has("result") && !jsonObject.isNull("result")) {
JSONObject resultObj = jsonObject.getJSONObject("result");
if (resultObj.has("errors") && !resultObj.isNull("errors")) {
JSONArray errorsArray = resultObj.getJSONArray("errors");
if (errorsArray.length() > 0) {
JSONObject errObj = errorsArray.getJSONObject(0);
if (errObj.has("message") && !errObj.isNull("message"))
return stringBuilder.append(errObj.getString("message")).toString();
}
}
}
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
}
}else{
return String.valueOf(stringBuilder.append("We are facing server issues please try again."));
}
}
return null;
}
注意这些
statusCode 将为您提供 int 错误代码,可能是 400 、 401 等等。
error.networkResponse.statusCode
如果存在,这将为您提供错误响应。
error.networkResponse.data
在你的情况下,尝试打印数据,看看你是否得到了状态代码和数据,如果你有这两种东西,那么你可以编写你的方法来解析错误,就像我根据你的错误消息格式所做的那样。
我需要帮助解决 Volley 库错误。我已经使用 nodejs api 向服务器发送了一些凭据以进行用户注册。在成功案例中,当我的所有凭据对于创建新用户帐户都是唯一的时,它会向我显示所有正确的所有响应等。但是当我发送一些重复的凭据时,意味着新用户的电子邮件与另一个用户的电子邮件相同,它无法显示我使用检查从服务器得到响应的服务器错误,但它显示我截击错误侦听器错误。
我想在这里显示来自服务器的错误,而不是来自 volley 错误的错误listener.Kindly给我解决方案,我现在卡住了。
代码如下:
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (firstName.getText().toString().isEmpty() || firstName.getText().toString().length() <= 3){
Toast.makeText(getContext(), "First name length is too short", Toast.LENGTH_SHORT).show();
}
else if (lastName.getText().toString().isEmpty() || lastName.getText().toString().length() <= 2){
Toast.makeText(getContext(), "Last name length is too short", Toast.LENGTH_SHORT).show();
}
else if (emailUser.getText().toString().isEmpty() || !emailUser.getText().toString().trim().matches(emailPattern)){
Toast.makeText(getContext(), "Enter valid email address", Toast.LENGTH_SHORT).show();
}
else if (mobileNumber.getText().toString().isEmpty() || mobileNumber.getText().toString().length() <= 8){
Toast.makeText(getContext(), "Enter valid mobile number", Toast.LENGTH_SHORT).show();
}
else if (dobUser.getText().toString().isEmpty()){
Toast.makeText(getContext(), "Select date of birth", Toast.LENGTH_SHORT).show();
}
else if (genderUser.getText().toString().isEmpty()){
Toast.makeText(getContext(), "Select gender", Toast.LENGTH_SHORT).show();
}
else if (passwordUser.getText().toString().isEmpty() || !passwordUser.getText().toString().trim().matches(PASSWORD_PATTERN)){
Toast.makeText(getContext(), "Enter valid password.Password must be 1 special character 1 lower letter and 1 Upper letter and 1 Number digit", Toast.LENGTH_SHORT).show();
}
else {
registerUser();
}
}
});
这是我想要的错误无法正确显示的功能,我也在代码中使用检查:
private void registerUser() {
final ProgressDialog progress = new ProgressDialog(getContext());
progress.setTitle("Register Account");
progress.setMessage("Loading...");
progress.setCancelable(false);
progress.show();
RequestQueue queue = Volley.newRequestQueue(getContext());
final JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("first_name", firstName.getText().toString());
jsonObject.put("last_name",lastName.getText().toString());
jsonObject.put("email", emailUser.getText().toString());
jsonObject.put("password", passwordUser.getText().toString());
jsonObject.put("gender",genderUser.getText().toString());
jsonObject.put("phone_number", mobileNumber.getText().toString());
jsonObject.put("date_of_birth", dobUser.getText().toString());
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String status = response.getString("status");
String message = response.getString("message");
if (status.equals("Success")){
progress.dismiss();
Toast.makeText(getContext(), response.toString(), Toast.LENGTH_SHORT).show();
}
else if (status.equals("Fail")){
JSONArray jsonArray = response.getJSONArray("data");
JSONObject jsonObject1 = jsonArray.getJSONObject(0);
String error = jsonObject1.getString("error");
progress.dismiss();
Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
else {
progress.dismiss();
Toast.makeText(getContext(), "Something went wrong.Please try again", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progress.dismiss();
Toast.makeText(getContext(), "Email or mobile number already used by other account.", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
// RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
// queue.add(request);
queue.add(jsonRequest);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getContext(), "Here is the error", Toast.LENGTH_SHORT).show();
}
}
这是来自服务器的错误 json 响应:
{
"status": "Fail",
"message": "Invalid Request",
"data": [
{
"error": "Email already used by another account!"
}
]
}
这是我的 onErrorResponse 方法中的行
String result = Global.parseError(error.networkResponse.statusCode, error.networkResponse.data);
这是我的 parseError 方法中的内容。
public static String parseError(int responseCode, byte[] data) {
StringBuilder stringBuilder = new StringBuilder();
if (data == null)
return null;
else {
if (responseCode == 400 || responseCode == 401 || responseCode == 402 || responseCode == 403 || responseCode == 404) {
String string = new String(data);
Log.d(TAG, "parseError: "+string);
try {
JSONObject jsonObject = new JSONObject(string);
Log.d(TAG, "parseError: " + jsonObject);
if (jsonObject.has("message") && !jsonObject.isNull("message"))
stringBuilder.append(jsonObject.getString("message"));
if (jsonObject.has("result") && !jsonObject.isNull("result")) {
JSONObject resultObj = jsonObject.getJSONObject("result");
if (resultObj.has("errors") && !resultObj.isNull("errors")) {
JSONArray errorsArray = resultObj.getJSONArray("errors");
if (errorsArray.length() > 0) {
JSONObject errObj = errorsArray.getJSONObject(0);
if (errObj.has("message") && !errObj.isNull("message"))
return stringBuilder.append(errObj.getString("message")).toString();
}
}
}
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
}
}else{
return String.valueOf(stringBuilder.append("We are facing server issues please try again."));
}
}
return null;
}
注意这些 statusCode 将为您提供 int 错误代码,可能是 400 、 401 等等。
error.networkResponse.statusCode
如果存在,这将为您提供错误响应。
error.networkResponse.data
在你的情况下,尝试打印数据,看看你是否得到了状态代码和数据,如果你有这两种东西,那么你可以编写你的方法来解析错误,就像我根据你的错误消息格式所做的那样。