在Retrofit 2.1中传递JSON数组

时间:2016-09-22 13:57:49

标签: retrofit

  

如何使用Retrofit 2.1在API调用中传递JsonArray   JSON

{
"address":[
    {
        "addressType":"Home",
        "addressLine1":"M12/150, Vidhyanagar Flates",
        "addressLine2": "Op. Himmatlal Park, Satelite",
        "city":"Ahmedabad",
        "State":"Gujarat",
        "zipCode":"380015",
        "country_id":"109",
        "latitude":"23.13213",
        "longitude":"72.313213",
        "isDefault":"0"
    }
]

}

响应

{
  "status": 1,
  "message": "Address stored successfully.",
  "result": 
[
{
  "id": 16,
  "user_id": 33,
  "addressType": "Home",
  "addressLine1": "M12/150, Vidhyanagar Flates",
  "addressLine2": "Op. Himmatlal Park, Satelite",
  "city": "Ahmedabad",
  "state": "Gujarat",
  "zipCode": "380015",
  "country_id": 109,
  "latitude": "23.13213",
  "longitude": "72.313213",
  "isDefault": 0,
  "created_at": "2016-09-22 06:37:43",
  "updated_at": "2016-09-22 06:37:43",
  "deleted_at": null
}

}

  

Pojo Class   1.Address.java

public class Address {

@SerializedName("status")
@Expose
private int status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("result")
@Expose
private ArrayList<AddressDetail> addressDetail = new ArrayList<AddressDetail>();

/**
 *
 * @return
 * The status
 */
public int getStatus() {
    return status;
}

/**
 *
 * @param status
 * The status
 */
public void setStatus(int status) {
    this.status = status;
}

/**
 *
 * @return
 * The message
 */
public String getMessage() {
    return message;
}

/**
 *
 * @param message
 * The message
 */
public void setMessage(String message) {
    this.message = message;
}

/**
 *
 * @return
 * The addressDetail
 */
public List<AddressDetail> getAddressDetail() {
    return addressDetail;
}

/**
 *
 * @param addressDetail
 * The addressDetail
 */
public void setAddressDetail(ArrayList<AddressDetail> addressDetail) {
    this.addressDetail = addressDetail;
}

}

  

2.AddressDetail.java

public class AddressDetail {

    @SerializedName("id")
    @Expose
    private int id;
    @SerializedName("user_id")
    @Expose
    private int userId;
    @SerializedName("addressType")
    @Expose
    private String addressType;
    @SerializedName("addressLine1")
    @Expose
    private String addressLine1;
    @SerializedName("addressLine2")
    @Expose
    private String addressLine2;
    @SerializedName("city")
    @Expose
    private String city;
    @SerializedName("state")
    @Expose
    private String state;
    @SerializedName("zipCode")
    @Expose
    private String zipCode;
    @SerializedName("country_id")
    @Expose
    private int countryId;
    @SerializedName("latitude")
    @Expose
    private String latitude;
    @SerializedName("longitude")
    @Expose
    private String longitude;
    @SerializedName("isDefault")
    @Expose
    private int isDefault;
    @SerializedName("created_at")
    @Expose
    private String createdAt;
    @SerializedName("updated_at")
    @Expose
    private String updatedAt;
    @SerializedName("deleted_at")
    @Expose
    private Object deletedAt;

    /**
     *
     * @return
     * The id
     */
    public int getId() {
            return id;
    }

    /**
     *
     * @param id
     * The id
     */
    public void setId(int id) {
            this.id = id;
    }

    /**
     *
     * @return
     * The userId
     */
    public int getUserId() {
            return userId;
    }

    /**
     *
     * @param userId
     * The user_id
     */
    public void setUserId(int userId) {
            this.userId = userId;
    }

    /**
     *
     * @return
     * The addressType
     */
    public String getAddressType() {
            return addressType;
    }

    /**
     *
     * @param addressType
     * The addressType
     */
    public void setAddressType(String addressType) {
            this.addressType = addressType;
    }

    /**
     *
     * @return
     * The addressLine1
     */
    public String getAddressLine1() {
            return addressLine1;
    }

    /**
     *
     * @param addressLine1
     * The addressLine1
     */
    public void setAddressLine1(String addressLine1) {
            this.addressLine1 = addressLine1;
    }

    /**
     *
     * @return
     * The addressLine2
     */
    public String getAddressLine2() {
            return addressLine2;
    }

    /**
     *
     * @param addressLine2
     * The addressLine2
     */
    public void setAddressLine2(String addressLine2) {
            this.addressLine2 = addressLine2;
    }

    /**
     *
     * @return
     * The city
     */
    public String getCity() {
            return city;
    }

    /**
     *
     * @param city
     * The city
     */
    public void setCity(String city) {
            this.city = city;
    }

    /**
     *
     * @return
     * The state
     */
    public String getState() {
            return state;
    }

    /**
     *
     * @param state
     * The state
     */
    public void setState(String state) {
            this.state = state;
    }

    /**
     *
     * @return
     * The zipCode
     */
    public String getZipCode() {
            return zipCode;
    }

    /**
     *
     * @param zipCode
     * The zipCode
     */
    public void setZipCode(String zipCode) {
            this.zipCode = zipCode;
    }

    /**
     *
     * @return
     * The countryId
     */
    public int getCountryId() {
            return countryId;
    }

    /**
     *
     * @param countryId
     * The country_id
     */
    public void setCountryId(int countryId) {
            this.countryId = countryId;
    }

    /**
     *
     * @return
     * The latitude
     */
    public String getLatitude() {
            return latitude;
    }

    /**
     *
     * @param latitude
     * The latitude
     */
    public void setLatitude(String latitude) {
            this.latitude = latitude;
    }

    /**
     *
     * @return
     * The longitude
     */
    public String getLongitude() {
            return longitude;
    }

    /**
     *
     * @param longitude
     * The longitude
     */
    public void setLongitude(String longitude) {
            this.longitude = longitude;
    }

    /**
     *
     * @return
     * The isDefault
     */
    public int getIsDefault() {
            return isDefault;
    }

    /**
     *
     * @param isDefault
     * The isDefault
     */
    public void setIsDefault(int isDefault) {
            this.isDefault = isDefault;
    }

    /**
     *
     * @return
     * The createdAt
     */
    public String getCreatedAt() {
            return createdAt;
    }

    /**
     *
     * @param createdAt
     * The created_at
     */
    public void setCreatedAt(String createdAt) {
            this.createdAt = createdAt;
    }

    /**
     *
     * @return
     * The updatedAt
     */
    public String getUpdatedAt() {
            return updatedAt;
    }

    /**
     *
     * @param updatedAt
     * The updated_at
     */
    public void setUpdatedAt(String updatedAt) {
            this.updatedAt = updatedAt;
    }

    /**
     *
     * @return
     * The deletedAt
     */
    public Object getDeletedAt() {
            return deletedAt;
    }

    /**
     *
     * @param deletedAt
     * The deleted_at
     */
    public void setDeletedAt(Object deletedAt) {
            this.deletedAt = deletedAt;
    }

}

  

如何使用Retrofit 2.1调用Api。不知道如何传递JsonArray。   我称之为API

@POST("address")
Call<Address> AddressCall(@Body Map<String, String> map);

1 个答案:

答案 0 :(得分:1)

为什么要传递map<String,String>?如果您发送一个重新呈现数据的对象,那就更好了。像AddresseDetails这样的东西,对于null属性,你可以配置解析器。

我认为这将成为:

@POST("address") Call<Address> AddressCall(@Body List<AddressDetail> newAddresses);