将不同的数据传递给Retrofit模型中相同名称的JSON属性。

huangapple go评论77阅读模式
英文:

Passing different data to same name json attribute in retrofit model

问题

public class Response {
    private boolean success;
    private String message;
    private Data data;

    public boolean isSuccess() {
        return success;
    }

    public String getMessage() {
        return message;
    }

    public Data getData() {
        return data;
    }
}

public class Data {
    // Define fields based on the possible data themes
    // For the first data theme
    private String update_status;
    private String last_version_name;
    private String last_version_code;
    private boolean need_to_upgrade;

    // For the second data theme
    private String year;
    private String month;
    private String day;
    private String hour;
    private String second;
    private String unix;

    // Generate getters for each field
    // ...
}
英文:

There is a WebService which i want to @GET and @POST data from it, but all of its responses are like this theme:

 {
  "data": {
    ...
  },
  "success": ... ,
  "message": ...
}

but there are different output themes of data, like these:

{
  "data": {
    "update_status": "critical",
    "last_version_name": "1.0.3",
    "last_version_code": "3",
    "need_to_upgrade": false
  },
  "success": true,
  "message": "DONE!"
}

&

{
  "data": {
    "year": "2020",
    "month": "09",
    "day": "09",
    "hour": "06",
    "second": "00",
    "unix": "1599631246"
  },
  "success": true,
  "message": null
}

How can i define models to use this via Retrofit and GSON Converter?
Should i use @SerializedName somewhere?

(i.e:)

public class Response{
    private boolean success;
    private String message;
    
    //private Data data
}

答案1

得分: 1

创建一个基础合约,并通过所有响应类进行扩展。在合约中,data 部分将是通用的。

public class BaseResponse<T> {
    public boolean success;
    public String message;
    public T data;
}

现在,你需要为每种类型的响应创建数据类。

public class MyResponse {
    public String update_status;
    public String last_version_name;
    public String last_version_code;
    public boolean need_to_upgrade;
}

现在你可以将 BaseResponse<MyResponse> 用作 Retrofit 调用的返回类型。对于数组响应,你也可以做同样的事情,即创建一个 BaseListResponse 类。

英文:

Create a Base Contract and extend it by all of the response classes. in Contract data part will be Generic.

public class BaseResponse&lt;T&gt; {
public boolean success;
public String message;
public T data;
 }

Now you have to create data class for each type of response.

public class MyResponse{
public String update_status;
public String last_version_name;
public String last_version_code;
public boolean need_to_upgrade;
}

Now you can use BaseResponse&lt;MyResponse&gt; as return type of retrofit call. You can do the same for Array response i.e create a BaseListResponse class.

huangapple
  • 本文由 发表于 2020年9月9日 14:07:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63805644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定