JSON
成功代码== 200时
{
"data": {
"user": {
"id": 8,
"name": "soleekuser",
"email": "soleekuser@gmail.com",
"job_title": "back end developer",
"active": 1,
"profile_pic": "http://52.174.22.188/soleekappapi/public/storage/profile_pics/Capture_1544361563.jpeg",
"created_at": "2018-12-09 13:20:37"
},
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjgsImlzcyI6Imh0dHA6Ly81Mi4xNzQuMjIuMTg4L3NvbGVla2FwcGFwaS9wdWJsaWMvYXBpL3VzZXJzL2xvZ2luIiwiaWF0IjoxNTQ0NjA1MDQ3LCJleHAiOjE1NDU4MTQ2NDcsIm5iZiI6MTU0NDYwNTA0NywianRpIjoiQk1sWTJ5NlVma043V0VhayJ9.npPbsHtV7G65jauwxltyvqS_xP7TmJetP9bTfTd9GB8"
},
"message": "Login Successfully",
"error": null
}
当我尝试使用错误的凭据(电子邮件或密码)进行身份验证时 响应码== 400(或任何其他非200码)
{
"message": "invalid username or password",
"error": "invalid username or password"
}
模型类
public class Employee implements Serializable {
@SerializedName("data")
@Expose
private Data data;
@SerializedName("message")
@Expose
private String message;
@SerializedName("error")
@Expose
private String error;
public Employee() {
}
public Employee(Data data, String message, String error) {
this.message = message;
this.error = error;
this.data = data;
}
// Getters & Setters
}
当缺少“数据”对象时,我应该怎么做才能将返回的JSON转换为模型类? (响应代码!= 200)
我认为即使缺少属性,Retrofit也会自动将响应json转换为模型!
更重要的是
call.enqueue(new Callback<Employee>() {
@Override
public void onResponse(Call<Employee> call, Response<Employee> response) {
Log.e(TAG_FRAG_LOGIN, "Response code -> " + response.code() + " " + response.message() + " ");
if (response.code() == 200) {
if (response.body() != null) {
// do stuff
// Everything works just fine when response code == 200, the body is not null.
}
} else if (response.code() != 200){
Log.d(TAG_FRAG_LOGIN, "Response code ------> " + response.code() + " " + response.message() + " " + call.request().toString());
if (response.body() != null) {
// do stuff
// When response code != 200 the (response.body()) is always null
}
}
为什么当响应代码!= 200时respnse.body()始终为null?
答案 0 :(得分:3)
尝试使常见的方法来处理错误消息。 就像这样。
/**
* this method used method response give error.
*
* @param context
* @param response
*/
public static void getResponseErrorMessage(Context context, String str, Response response) {
if (response.code() == 401 || response.code() == 400) {
try {
Gson gson = new Gson();
CommonErrorModel errorModel = gson.fromJson(response.errorBody().string(), CommonErrorModel.class);
showAlertDialog(context, str, errorModel.message == null ? context.getString(R.string.something_went_wrong) : errorModel.message);
} catch (IOException e) {
e.printStackTrace();
}
} else if (response.code() == 500) {
showAlertDialogMessage(context, str, context.getString(R.string.something_went_wrong));
} else {
showAlertDialogMessage(context, str, context.getString(R.string.something_went_wrong));
}
}
此方法称为api响应。onResponse()方法。
if (response != null && response.isSuccessful() && response.body() != null) {
}
else{
getResponseErrorMessage(mContext, getString(R.string.app_name), response);
}
还使错误消息像这样的pojo类。
public class CommonErrorModel {
@SerializedName("status")
public int status;
@SerializedName("message")
public String message;
}
答案 1 :(得分:0)
当响应中缺少数据对象时,它被认为与用于创建模型的响应不同。这就是为什么它为空的原因。没有数据时,可以从Web服务传递空白数据对象。这意味着您将获得与状态为200时相同的响应,但数据对象将没有数据