如何在改造中从Array获得响应?

时间:2018-08-28 12:09:10

标签: java android kotlin retrofit

这是我的回复。我不知道如何为这种类型的响应模型创建响应模型

[{"id":"4","templateName":"FUP 100","dataUsage":"100 GB","price":236,"groupName":"","bandwidthName":""},{"id":"19","templateName":"FUP200","dataUsage":"200 GB","price":299.72,"groupName":"","bandwidthName":""}]

2 个答案:

答案 0 :(得分:2)

您的改造调用必须是对象的列表,而不仅仅是对象

您的对象就是这样

public class MyClass
{
    private String id;

    private String groupName;

    private String price;

    private String dataUsage;

    private String bandwidthName;

    private String templateName;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getGroupName ()
    {
        return groupName;
    }

    public void setGroupName (String groupName)
    {
        this.groupName = groupName;
    }

    public String getPrice ()
    {
        return price;
    }

    public void setPrice (String price)
    {
        this.price = price;
    }

    public String getDataUsage ()
    {
        return dataUsage;
    }

    public void setDataUsage (String dataUsage)
    {
        this.dataUsage = dataUsage;
    }

    public String getBandwidthName ()
    {
        return bandwidthName;
    }

    public void setBandwidthName (String bandwidthName)
    {
        this.bandwidthName = bandwidthName;
    }

    public String getTemplateName ()
    {
        return templateName;
    }

    public void setTemplateName (String templateName)
    {
        this.templateName = templateName;
    }

    @Override
    public String toString()
    {
        return "MyClass [id = "+id+", groupName = "+groupName+", price = "+price+", dataUsage = "+dataUsage+", bandwidthName = "+bandwidthName+", templateName = "+templateName+"]";
    }
}

kotlin:

class MyClass {
  var id:String
  var groupName:String
  var price:String
  var dataUsage:String
  var bandwidthName:String
  var templateName:String
  public override fun toString():String {
    return "MyClass [id = " + id + ", groupName = " + groupName + ", price = " + price + ", dataUsage = " + dataUsage + ", bandwidthName = " + bandwidthName + ", templateName = " + templateName + "]"
  }
}

有在线工具可以帮助您http://pojo.sodhanalibrary.com/

答案 1 :(得分:0)

public class Response {

@SerializedName("id")
@Expose
private String id;
@SerializedName("templateName")
@Expose
private String templateName;
@SerializedName("dataUsage")
@Expose
private String dataUsage;
@SerializedName("price")
@Expose
private Double price;
@SerializedName("groupName")
@Expose
private String groupName;
@SerializedName("bandwidthName")
@Expose
private String bandwidthName;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getTemplateName() {
    return templateName;
}

public void setTemplateName(String templateName) {
    this.templateName = templateName;
}

public String getDataUsage() {
    return dataUsage;
}

public void setDataUsage(String dataUsage) {
    this.dataUsage = dataUsage;
}

public Double getPrice() {
    return price;
}

public void setPrice(Double price) {
    this.price = price;
}

public String getGroupName() {
    return groupName;
}

public void setGroupName(String groupName) {
    this.groupName = groupName;
}

public String getBandwidthName() {
    return bandwidthName;
}

public void setBandwidthName(String bandwidthName) {
    this.bandwidthName = bandwidthName;
}

}

然后创建ArrayList,因为您的响应以array开头:

@Headers("Content-Type:application/json")
@GET("your_api")
Call<ArrayList<Response>> api();