我想使用改造从我的 Get api 获得响应,但我不知道如何检查 status:success
所以它在下面给出了我的 api 响应的完整代码时显示空响应,
我的回复是:
{
status: "success",
data: [
{
Country_id: "1",
Country_name: "Maldives",
Continent_name: "Asia",
}
]
}
我的接口(interface)类:
public interface GetDataService
{
@GET("/Webserices/country_random.php")
Call<List<RetroPhoto>> getAllPhotos();
}
POJO:
public class RetroPhoto
{
@SerializedName("Country_id")
private Integer id;
@SerializedName("Country_name")
private String title;
@SerializedName("Continent_name")
private String name;
public RetroPhoto(Integer id, String title, String name) {
this.id = id;
this.title = title;
this.name= name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
下面是 MainActivity 类中的调用方法:
GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
Call<List<RetroPhoto>> call = service.getAllPhotos();
call.enqueue(new Callback<List<RetroPhoto>>() {
@Override
public void onResponse(Call<List<RetroPhoto>> call, Response<List<RetroPhoto>> response) {
progressDoalog.dismiss();
Log.e("jellosdsd", "onResponse:********************* " + response.body());
}
@Override
public void onFailure(Call<List<RetroPhoto>> call, Throwable t) {
progressDoalog.dismiss();
Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
上面给出了完整的代码,但它显示了空响应,因为我不知道如何检查 status: "success"
和 retrofit 中的数据数组对象。
所以请任何人解释一下如何在改造中检查它。
谢谢。
最佳答案
尝试创建您的自定义响应类。它应该看起来像这样:
public class RetroPhotoResponse {
private String status;
private List<RetroPhoto> data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<RetroPhoto> getData() {
return data;
}
public void setData(List<RetroPhoto> data) {
this.data = data;
}
关于android - 如何使用改造从 GET url 获得响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52040660/