我有一个Api,它返回两个不同的响应。
成功后,响应为-
{
"status": "failed",
"message": "",
"errors": []
}
失败后,响应为-
Call<Object> call = jsonApi.hitOtp(otpPost);
call.enqueue(new Callback<Object>() {
@Override
public void onResponse(Call<Object> call, Response<Object> response) {
Log.d(TAG, "onResponse: "+response.body());
if(response.body() instanceof OtpSuccessResponse){
Toast.makeText(OtpActivity.this, otpSuccRes.getMessage(), Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(OtpActivity.this,"Failed", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Object> call, Throwable t) {
Log.d(TAG, "onFailure: "+t.getMessage());
}
});
这是我处理数据解析的方式-
java.lang.ClassCastException
com.google.gson.internal.LinkedTreeMap cannot be cast to
com.avaskm.yellowalleyproject.Retrofit.Otp.OtpSuccessResponse
我在日志中收到成功响应,但是if条件不起作用。相反,我得到了-
public class OtpSuccessResponse {
@SerializedName("status")
@Expose
private String status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("token")
@Expose
private String token;
@SerializedName("subscription_active")
@Expose
private String subscriptionActive;
public OtpSuccessResponse(String status, String message, String token, String subscriptionActive) {
this.status = status;
this.message = message;
this.token = token;
this.subscriptionActive = subscriptionActive;
}
}
我的OnSuccess Pojo课堂-
const options = {
identityMetadata: process.env.AZUREAD_IDENTITY_METADATA,
clientID: process.env.AZUREAD_AUTH_CLIENT_ID,
responseType: 'code id_token',
responseMode: 'form_post',
redirectUrl: process.env.AZUREAD_REDIRECT_URL,
allowHttpForRedirectUrl: true,
clientSecret: process.env.AZUREAD_AUTH_CLIENT_SECRET,
validateIssuer: false,
issuer: null,
passReqToCallback: true,
useCookieInsteadOfSession: true,
cookieEncryptionKeys: [
{ key: '********************************', iv: '************' },
{ key: '********************************', iv: '************' },
],
scope: ['profile', 'offline_access', 'https://graph.microsoft.com/mail.read'],
loggingLevel: 'info',
nonceLifetime: null,
nonceMaxAmount: 5,
clockSkew: null,
};