gpt4 book ai didi

android - 在Android中使用Volley进行错误处理

转载 作者:行者123 更新时间:2023-12-03 08:24:08 24 4
gpt4 key购买 nike

我想使用android中的onErrorResponsevolley打印我的错误,我想以不同的separately打印它们textview

我的错误从onErrorResponse

{
"message": "422 Unprocessable Entity",
"error": {

"username": [
"The username has already been taken."
],
"email": [
"The email has already been taken."
]
},
"status_code": 422
}

所以我想分别打印
我的意思是在一个 The username has already been taken.中的 textview和在第二个 The email has already been taken.中的 textview。谢谢

我的代码:
public void postData(JSONObject jsonObject) {       
String url = "http://www.xxxxxxxx.com/api/v1/auth/register";
String REQUEST_TAG = "volley_key";
JsonObjectRequest jsonObjectReq = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {

try {
verifyResponse(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null && networkResponse.data != null) {
String errorStr = new String(networkResponse.data);
try {
JSONObject jObj = new JSONObject(errorStr);
JSONObject objError = jObj.getJSONObject("error");
JSONArray emailArray = objError.getJSONArray("email");
if (emailArray != null) {
String emailMessage = String.valueOf(emailArray.get(0));
Toast.makeText(getApplication(), emailMessage, Toast.LENGTH_LONG).show();
}
JSONArray usernameArray = objError.getJSONArray("username");
if (usernameArray != null) {
String usernameMessage = String.valueOf(emailArray.get(0));
Toast.makeText(getApplication(), usernameMessage, Toast.LENGTH_LONG).show();
}

} catch (JSONException e) {
e.printStackTrace();
}
}
}
}){
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {

if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));
volleyError = error;
}
return volleyError;
}
}; VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectReq, REQUEST_TAG);
}

最佳答案

将那里的ErrorModel.class和Error类添加到java文件夹中。

package co.exmaple;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ErrorModel {

@SerializedName("message")
@Expose
private String message;
@SerializedName("error")
@Expose
private Error error;
@SerializedName("status_code")
@Expose
private Integer statusCode;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Error getError() {
return error;
}

public void setError(Error error) {
this.error = error;
}

public Integer getStatusCode() {
return statusCode;
}

public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}

}

错误类
package co.exmaple;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Error {

@SerializedName("name")
@Expose
private List<String> name = null;
@SerializedName("username")
@Expose
private List<String> username = null;
@SerializedName("email")
@Expose
private List<String> email = null;

public List<String> getName() {
return name;
}

public void setName(List<String> name) {
this.name = name;
}

public List<String> getUsername() {
return username;
}

public void setUsername(List<String> username) {
this.username = username;
}

public List<String> getEmail() {
return email;
}

public void setEmail(List<String> email) {
this.email = email;
}

}

然后在StringError请求中将Error文本设置为texView。
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {



}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
ErrorModel error = gson.fromJson(response,ErrorModel.class);
recyclerView.setAdapter(new ErrorAdapter(ErrorActivity.this,error.getError));
}
});

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);

使RecyclerView适配器将错误文本设置为TextView

关于android - 在Android中使用Volley进行错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47406363/

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