我有一个严格的API,它将根据请求是否正确为我提供不同的JSON。对于一个相同的请求,我可以使用2个不同的JSON。请求的状态始终为200(如果错误或成功),所以我无法使用请求的状态
Error response :
{
"stError": {
"azType": "Application",
"azCode": "Bad value parameter - azPassword is empty"
}
}
Success response :
{
"azToken": "qsdsqd", #1 And this field can be inexistant if the bLogin is 0
"bLogin" : 1
}
因此,我创建了所需的 POJO :
public class LoginAPIResponse {
@SerializedName("azToken")
@Expose
private String azToken;
@SerializedName("bLogin")
@Expose
private Boolean bLogin;
### Setters and getters stuff ###
}
public class LOLError {
@SerializedName("stError")
@Expose
private stError stError;
### Setters and getters stuff ###
}
public class stError {
@SerializedName("azType")
@Expose
private String azType;
@SerializedName("azCode")
@Expose
private String azCode;
### Setters and getters stuff ###
}
当然,当我创建WebService时,我需要我的服务
public interface LoginService {
/***
* @POST declares an HTTP POST Request
* This request is use when a user needs to login with his credentials
*/
@POST("JCExecDev/JCOSSUserLogin")
Call<LOLError> login(@Body LoginAPIRequest request);
}
这是我在存储库中的登录功能:
Call</*[DynamicResponse?]*/> call = loginService.login(request);
call.enqueue(new Callback</*[DynamicResponse?]*/>() {
@Override
public void onResponse(Call</*[DynamicResponse?]*/> call, Response<LOLError> response) {
call.enqueue(new Callback</*[DynamicResponse?]*/>() {
@Override
public void onResponse(Call</*[DynamicResponse?]*/> call, Response<LOLError> response) {
// Manage to find the way to get either LOLError or LoginAPIResponse
}
@Override
public void onFailure(Call</*[DynamicResponse?]*/> call, Throwable t) {
#Failure from the server, we do not take this in consideration
}
});
我正在使用带有GSON反序列化的改造2。
这是我的问题:
我无法更改程序,这意味着我无法更改响应,也无法为错误和成功添加公用字段。