如何使用改型从GET网址获得响应

时间:2018-08-27 13:36:00

标签: android

我想通过改造从Get api获得响应,但是我不知道如何检查status:success,因此在给定完整代码的情况下,它在api响应下显示空响应,

我的回复是:

{
   status: "success",
   data: [
           {
            Country_id: "1",
            Country_name: "Maldives",
            Continent_name: "Asia",
           }
]
}

我的界面类:

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"和数据数组对象。

所以请任何人解释一下如何进行翻新检查。

谢谢。

1 个答案:

答案 0 :(得分:1)

尝试创建您的自定义响应类。它看起来应该像这样:

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;
}

}