【问题标题】:how to catch the api response in retrofit2 for two arrays of different objects? [duplicate]如何在 Retrofit2 中捕获两个不同对象数组的 api 响应? [复制]
【发布时间】:2017-09-25 12:15:53
【问题描述】:

如何在 Retrofit2 中捕捉到以下响应?它有两种不同的对象类型。从 api 检索后如何存储和使用它? 这是我的 api 响应的格式

{
"products": [
    {
        "categoryID": 2,
        "companyID": 1,
        "companyName": "Audi",
        "productID": 1001,
        "productName": "S6"
    },
    {
        "categoryID": 4,
        "companyID": 1,
        "companyName": "BWM",
        "productID": 1001,
        "productName": "Zen"
    },
    {
        "categoryID": 5,
        "companyID": 1,
        "companyName": "Ford",
        "productID": 1001,
        "productName": "Mustang"
    }],
"solutions": [
    {
        "companyID": 2,
        "companyName": "Audi",
        "solutionName": "A good plan",
        "solutionType": "Personalised",
        "working": "dsjhfgsdjhfgjsdh"
    },
    {
        "companyID": 2,
        "companyName": "djfhgkkl",
        "solutionName": "A good plan",
        "solutionType": "Personalised",
        "working": "asfh"
    }
]
}

【问题讨论】:

    标签: java android api retrofit2


    【解决方案1】:

    您可以创建一个 POJO 类并从对象中获取数据 这是 POJO

    package com.example;
    
    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Example {
    
        @SerializedName("products")
        @Expose
        private List<Product> products = null;
        @SerializedName("solutions")
        @Expose
        private List<Solution> solutions = null;
    
        public List<Product> getProducts() {
            return products;
        }
    
        public void setProducts(List<Product> products) {
            this.products = products;
        }
    
        public List<Solution> getSolutions() {
            return solutions;
        }
    
        public void setSolutions(List<Solution> solutions) {
            this.solutions = solutions;
        }
    }
    -------------------------------com.example.Product.java-------------------------------
    
    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Product {
        @SerializedName("categoryID")
        @Expose
        private Integer categoryID;
        @SerializedName("companyID")
        @Expose
        private Integer companyID;
        @SerializedName("companyName")
        @Expose
        private String companyName;
        @SerializedName("productID")
        @Expose
        private Integer productID;
        @SerializedName("productName")
        @Expose
        private String productName;
    
        public Integer getCategoryID() {
            return categoryID;
        }
    
        public void setCategoryID(Integer categoryID) {
            this.categoryID = categoryID;
        }
    
        public Integer getCompanyID() {
            return companyID;
        }
    
        public void setCompanyID(Integer companyID) {
            this.companyID = companyID;
        }
    
        public String getCompanyName() {
            return companyName;
        }
    
        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }
    
        public Integer getProductID() {
            return productID;
        }
    
        public void setProductID(Integer productID) {
            this.productID = productID;
        }
    
        public String getProductName() {
            return productName;
        }
    
        public void setProductName(String productName) {
            this.productName = productName;
        }
    }
    -------------------------------com.example.Solution.java-------------------------------
    
    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Solution {
        @SerializedName("companyID")
        @Expose
        private Integer companyID;
        @SerializedName("companyName")
        @Expose
        private String companyName;
        @SerializedName("solutionName")
        @Expose
        private String solutionName;
        @SerializedName("solutionType")
        @Expose
        private String solutionType;
        @SerializedName("working")
        @Expose
        private String working;
    
        public Integer getCompanyID() {
            return companyID;
        }
    
        public void setCompanyID(Integer companyID) {
            this.companyID = companyID;
        }
    
        public String getCompanyName() {
            return companyName;
        }
    
        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }
    
        public String getSolutionName() {
            return solutionName;
        }
    
        public void setSolutionName(String solutionName) {
            this.solutionName = solutionName;
        }
    
        public String getSolutionType() {
            return solutionType;
        }
    
        public void setSolutionType(String solutionType) {
            this.solutionType = solutionType;
        }
    
        public String getWorking() {
            return working;
        }
    
        public void setWorking(String working) {
            this.working = working;
        }
    }
    

    您可以使用 http://www.jsonschema2pojo.org/ 从 JSON 生成 POJO

    【讨论】:

      【解决方案2】:

      您可以在此处使用 Gson 解析并为产品和解决方案数组添加 @Nullable 注释。在使用这些数据的同时检查 arraylist 的大小并检测列表中存在哪个数组。

      【讨论】:

        【解决方案3】:

        将您的回复复制并粘贴到 Jsonschema2Pojo获取您回复的愿望Pojo。

        【讨论】:

          猜你喜欢
          • 2021-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-01
          • 1970-01-01
          • 2018-05-09
          • 2019-04-23
          • 2021-11-20
          相关资源
          最近更新 更多