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

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

Passing different data to same name json attribute in retrofit model

问题

  1. public class Response {
  2. private boolean success;
  3. private String message;
  4. private Data data;
  5. public boolean isSuccess() {
  6. return success;
  7. }
  8. public String getMessage() {
  9. return message;
  10. }
  11. public Data getData() {
  12. return data;
  13. }
  14. }
  15. public class Data {
  16. // Define fields based on the possible data themes
  17. // For the first data theme
  18. private String update_status;
  19. private String last_version_name;
  20. private String last_version_code;
  21. private boolean need_to_upgrade;
  22. // For the second data theme
  23. private String year;
  24. private String month;
  25. private String day;
  26. private String hour;
  27. private String second;
  28. private String unix;
  29. // Generate getters for each field
  30. // ...
  31. }
英文:

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

  1. {
  2. "data": {
  3. ...
  4. },
  5. "success": ... ,
  6. "message": ...
  7. }

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

  1. {
  2. "data": {
  3. "update_status": "critical",
  4. "last_version_name": "1.0.3",
  5. "last_version_code": "3",
  6. "need_to_upgrade": false
  7. },
  8. "success": true,
  9. "message": "DONE!"
  10. }

&

  1. {
  2. "data": {
  3. "year": "2020",
  4. "month": "09",
  5. "day": "09",
  6. "hour": "06",
  7. "second": "00",
  8. "unix": "1599631246"
  9. },
  10. "success": true,
  11. "message": null
  12. }

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

(i.e:)

  1. public class Response{
  2. private boolean success;
  3. private String message;
  4. //private Data data
  5. }

答案1

得分: 1

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

  1. public class BaseResponse<T> {
  2. public boolean success;
  3. public String message;
  4. public T data;
  5. }

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

  1. public class MyResponse {
  2. public String update_status;
  3. public String last_version_name;
  4. public String last_version_code;
  5. public boolean need_to_upgrade;
  6. }

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

英文:

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

  1. public class BaseResponse&lt;T&gt; {
  2. public boolean success;
  3. public String message;
  4. public T data;
  5. }

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

  1. public class MyResponse{
  2. public String update_status;
  3. public String last_version_name;
  4. public String last_version_code;
  5. public boolean need_to_upgrade;
  6. }

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:

确定