1

How do i catch the below response in retrofit2? It has two different object types. And after retrieving from the api how do I store and use it? This the format of my api response

{
"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"
    }
]
}
vinitpradhan18
  • 51
  • 1
  • 10

3 Answers3

1

you can create a POJO class and from the object, you can get the data here is the 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;
    }
}

and you can generate your POJO from JSON using http://www.jsonschema2pojo.org/

Shubham Jain
  • 2,365
  • 2
  • 17
  • 29
0

You can use Gson parsing here and add @Nullable annotation for both products and solutions array. And while using this data check the size of arraylist and detect which array is present in the list.

0

Copy and pate your response to Jsonschema2Pojo to obtain desire Pojo of your response.

Amit Bhati
  • 1,405
  • 12
  • 20