Android 改装2.1.0中response.issucessfull()的条件不同,但仍在内部
我正在使用Jackson的改进版2.1.0来进行API调用。 远程api提供两种不同的响应,一种是带有字符串的json对象,另一种是自定义对象。另一个响应仅显示状态和错误消息。如下所示 所需响应Android 改装2.1.0中response.issucessfull()的条件不同,但仍在内部,android,retrofit2,Android,Retrofit2,我正在使用Jackson的改进版2.1.0来进行API调用。 远程api提供两种不同的响应,一种是带有字符串的json对象,另一种是自定义对象。另一个响应仅显示状态和错误消息。如下所示 所需响应 { "status": "success", "data": { "id": 79068, "username": "neha.fren01@gmail.com", "email": "neha.fren01@gmail.com", "enabled": fals
{
"status": "success",
"data": {
"id": 79068,
"username": "neha.fren01@gmail.com",
"email": "neha.fren01@gmail.com",
"enabled": false,
"confirmation_token": "27090745",
"name": "ronem",
"premium": false,
"token": "nmdgZe5Put6kS1KC_KH5d8Tk-fZLxZ15bV5tahJwQlY",
"phone_verified": false,
"email_verified": false
}
}
{
"status": "error",
"message": "Unable to create userERROR: Email you entered is invalid. Provide another email.\n"
}
错误响应
{
"status": "success",
"data": {
"id": 79068,
"username": "neha.fren01@gmail.com",
"email": "neha.fren01@gmail.com",
"enabled": false,
"confirmation_token": "27090745",
"name": "ronem",
"premium": false,
"token": "nmdgZe5Put6kS1KC_KH5d8Tk-fZLxZ15bV5tahJwQlY",
"phone_verified": false,
"email_verified": false
}
}
{
"status": "error",
"message": "Unable to create userERROR: Email you entered is invalid. Provide another email.\n"
}
我可以处理上面显示的所需响应。但无法处理错误响应
这是我打电话的密码
call.enqueue(new Callback<RegisteredResponse>() {
@Override
public void onResponse(Call<RegisteredResponse> call, Response<RegisteredResponse> response) {
if (response.isSuccessful()) {
try {
EventBus.post(new Events.RegistrationResponseEvent(StaticStorage.REGISTRATION_IDENTIFIER, response.body()));
} catch (Exception e) {
e.printStackTrace();
// EventBus.post(new Events.ErrorEvent(StaticStorage.REGISTRATION_IDENTIFIER, response.errorBody().toString()));
}
}else{
// I want to handle error response here.but error response is present inside response.isSuccessful() block
}
}
@Override
public void onFailure(Call<RegisteredResponse> call, Throwable t) {
t.printStackTrace();
EventBus.post(new Events.ErrorEvent(StaticStorage.REGISTRATION_IDENTIFIER, StaticStorage.ERROR_IN_LOADING));
}
});
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
试一试{
EventBus.post(新的Events.RegistrationResponseEvent(StaticStorage.REGISTRATION_标识符,response.body());
}捕获(例外e){
e、 printStackTrace();
//EventBus.post(newevents.ErrorEvent(StaticStorage.REGISTRATION_标识符,response.errorBody().toString());
}
}否则{
//我想在这里处理错误响应。但错误响应存在于response.issucessful()块中
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
t、 printStackTrace();
EventBus.post(newevents.ErrorEvent(StaticStorage.REGISTRATION\u标识符,StaticStorage.ERROR\u在加载中));
}
});
即使响应对象是错误响应,它也总是指向
响应。isSuccessful()
挡块
我无法解决这个问题。我花了2个小时寻找解决方案。
我也跟着林克
response.isSuccessful()
改装取决于您从服务器获得的状态代码。即使出现故障,如果服务器仍然向您发送200
响应代码
,那么您也无法在onError中获取它
表示您的api调用成功,并收到响应。不管来自服务器的响应是什么。为此,您必须手动解析响应,并检查您的Json
对象是否具有error
或success
状态。根据我的意见,您必须使响应对所有web服务通用。像
成功
错误
请更改为状态布尔标志,您必须检查响应的状态。如果为true,则响应成功如果为false,则响应中存在一些错误
response.isSuccessful()
您需要检查服务器响应的状态代码,如果是200,则成功调用web服务。您必须为错误响应设置响应代码,如404或500,以通知改造响应情况实际的错误代码是什么?,如果是http错误代码,有时api错误实现,导致“错误”响应仍发送200状态代码,则改装将仅调用onFailure。我希望在响应的else条件下处理不同的对象。isSuccessfull()@masoudvali我需要从android代码设置还是在服务器端设置?你应该在服务器端设置技术上不正确,服务器可以返回404等,它不会“成功”,但它仍然是一个服务器response@JoeMaher是的,认识到我的错误并据此改变。