我按照这个来使用 Retrofit2 发布数据
我使用JSON POJO来解析我的 POST 和 GET 文件
所以在这里如果记录中的数据在那里我会得到这种响应
{
"status": "200",
"response": [{
"cnt_id": "201",
"phn_no": "3251151515",
"dat_cnt": "Reset Password request Said to Mail"
},
{
"cnt_id": "209",
"phn_no": "555465484684",
"dat_cnt": "Hi DEMO User , Congratulations! Your account has been created successfully."
},
{
"cnt_id": "210",
"phn_no": "4774748",
"dat_cnt": "Hi XYZ , Congratulations! Your account has been created successfully."
}
]
}
如果没有数据我会得到
{"status":"204","response":{"msg":"No Content"}}
或
{"status":"400","response":{"msg":"BadRequest"}}
或
{"status":"401","response":{"msg":"Unauthorized User"}}
所以在这里我可以解析状态为 200 的数据,但是当状态不等于 200 时,我想处理它们
我试过了
status = response.body().getStatus();
if(status.equals("200")) {
List<Response> resList = response.body(). getResponse();
for(int i = 0; i<resList.size(); i++)
{.
.
..
.
}
}
else {
//not Implemented
}
现在我应该写什么否则我在 POJO 中使用了不等于 200 的响应数据,但我要求列表
更新
com.example.Example.java
public class Example {
@SerializedName("status") @Expose private String status;
@SerializedName("response") @Expose private List<Response> response = null;
}
com.example.Response.java
public class Response {
@SerializedName("cnt_id") @Expose private String cntId;
@SerializedName("phn_no") @Expose private String phnNo;
@SerializedName("dat_cnt") @Expose private String datCnt;
}