android - 如何使用改造从 GET url 获得响应

标签 android

我想使用改造从我的 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/

相关文章:

android - 从 getSupportActionBar() 获取 ImageView

Android NetworkStats.Bucket 数据未更新

java - 找不到参数的方法实现()

javascript - 使用 GPS 监测移动设备的位移

java - 如何将当前时间与Android中存储在数据库中的时间进行比较

java - 通过 SQLite Manager 更新我的 SQLite 数据库列后,为什么更改没有反射(reflect)在我的 Android 应用程序中?

android - Gradle 自定义插件 : The Task. leftShift(Closure) 方法已被弃用,计划在 Gradle 5.0 中删除

android - 如何获取设备的屏幕尺寸?

java.net.SocketException : Address family not supported by protocol 异常

android - 如何在android中裁剪横向视频的可见部分?