gpt4 book ai didi

java - 如何使用 Retrofit Android 解析对象内部的数组

转载 作者:行者123 更新时间:2023-11-29 09:58:47 25 4
gpt4 key购买 nike

我是 Retrofit 库的新手,我曾经使用过 Volley我正在尝试解析对象内的数组,但我不知道该怎么做这是我的 Json 响应

  {
"response": {
"code": "1",
"success": true,
"customers": [
{
"id": 1,
"name": "reem",
"customer_type": "1",
"address": "45سسسس",
"mobile_no": "05684412211",
"phone_no": "414511555",
"created_at": "2018-07-30 08:26:48",
"updated_at": "2018-07-30 08:26:48"
}
]
}
}

我想从响应响应中获取客户数组这是客户模型:

public class Customer {

@SerializedName("id")
private Integer id;
@SerializedName("customer_type")
private Integer customer_type;
@SerializedName("name")
private String name;
@SerializedName("address")
private String address;
@SerializedName("mobile_no")
private String mobile_no;
@SerializedName("phone_no")
private String phone_no;

public Customer(Integer id, Integer customer_type, String name, String address, String mobile_no, String phone_no) {
this.id = id;
this.customer_type = customer_type;
this.name = name;
this.address = address;
this.mobile_no = mobile_no;
this.phone_no = phone_no;
}

public Integer getId() {
return id;
}

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

public Integer getCustomer_type() {
return customer_type;
}

public void setCustomer_type(Integer customer_type) {
this.customer_type = customer_type;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getMobile_no() {
return mobile_no;
}

public void setMobile_no(String mobile_no) {
this.mobile_no = mobile_no;
}

public String getPhone_no() {
return phone_no;
}

public void setPhone_no(String phone_no) {
this.phone_no = phone_no;
}
}

这里是数据服务接口(interface):

@GET("get_customers")
Call<List<Customer>> getAllCustomer();

能否请您帮助我了解如何解析它,谢谢。

最佳答案

创建另一个 POJO 类,其中包含这样的列表

public class Response{

@SerializedName("response")
private Response response;

@SerializedName("code")
private String code;

@SerializedName("success")
private boolean success;

@SerializedName("customers")
private List<Customers> customers;

public void setResponse(Response response){
this.response = response;
}

public Response getResponse(){
return response;
}

public void setCode(String code){
this.code = code;
}

public String getCode(){
return code;
}

public void setSuccess(boolean success){
this.success = success;
}

public boolean isSuccess(){
return success;
}

public void setCustomers(List<Customers> customers){
this.customers = customers;
}

public List<Customers> getCustomers(){
return customers;
}
}

然后在你的数据服务接口(interface)

@GET("get_customers")
Call<Response> getAllCustomer();

然后retrofit call拿到body之后就可以得到这样的客户List

reponse.getCustomers();

关于java - 如何使用 Retrofit Android 解析对象内部的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52147481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com