我正在使用Retrofit和Parceler库,以便与我的服务器进行通信。
服务器具有以下两种API方法:
GET /api/metadata/{id}
返回以下JSON
{
"id": 1,
"active": true,
"location": {
"type": "Point",
"coordinates": [
30.0000,
30.0000
]
}
}
POST / api / metadata / {id}需要以下JSON
{
"id": 1,
"active": true,
"location_latitude": 30.0000,
"location_longitude": 30.0000
}
出于历史原因这是如此,不能改变。
在我的Android应用程序中,我通过以下方式声明Retrofit:
public interface ServerApi {
@GET("/api/metadata/{id}")
Metadata getMetadata(@Path("id") int id);
@POST("/api/metadata/{id}")
Metadata updateMetadata(@Path("id") int id, @Body Metadata metadata);
}
Parcel类按以下方式定义:
元数据:
@Parcel
public class Metadata {
@SerializedName("id")
private Integer id;
@SerializedName("location")
private GeometryPoint location;
@SerializedName("location_latitude")
private float locationLatitude;
@SerializedName("location_longitude")
private float locationLongitude;
public void setId(Integer id) {
this.id = id;
}
public void setLocationLatitude(float locationLatitude) {
this.locationLatitude = locationLatitude;
}
public void setLocationLongitude(float locationLongitude) {
this.locationLongitude = locationLongitude;
}
public void setLocation(GeometryPoint location) {
this.location = location;
}
public Integer getId() {
return id;
}
public float getLocationLatitude() {
return locationLatitude;
}
public float getLocationLongitude() {
return locationLongitude;
}
public GeometryPoint getLocation() {
return location;
}
}
GeometryPoint:
@Parcel
public class GeometryPoint {
@SerializedName("type")
private String type;
@SerializedName("coordinates")
private float[] coordinates;
public void setType(String type) {
this.type = type;
}
public void setCoordinates(float[] coordinates) {
this.coordinates = coordinates;
}
public String getType() {
return type;
}
public float[] getCoordinates() {
return coordinates;
}
}
我想在整个应用程序中使用Metadata类。我想查询服务器,接收元数据,更新它,并将其发送到服务器。显然,GET和POST之间的元数据格式不同。因此,我希望GET在收到它后转换为POST格式。
我的问题是,是否有可能以某种方式声明注释,以便Retrofit和Parceler知道location
参数,从JSON反序列化它,但通过Metadata
将其写入setLocation()
类我可以将其分解为“location_latitude”和“location_longitude”的方法。
这是所需元数据类的一些伪代码:
@Parcel
public class Metadata {
@SerializedName("id")
private Integer id;
// I'd like not having location variable defined at all
// but use some annotation magic :)) to tell GSON to deserialize
// JSON and call setLocation() when it tries to process location
// parameter of the server response
/*
@SerializedName("location")
private GeometryPoint location;
*/
@SerializedName("location_latitude")
private float locationLatitude;
@SerializedName("location_longitude")
private float locationLongitude;
public void setId(Integer id) {
this.id = id;
}
public void setLocationLatitude(float locationLatitude) {
this.locationLatitude = locationLatitude;
}
public void setLocationLongitude(float locationLongitude) {
this.locationLongitude = locationLongitude;
}
public void setLocation(GeometryPoint location) {
this.location_latitude = location.getCoordinates()[1];
this.location_longitude = location.getCoordinates()[0];
}
public Integer getId() {
return id;
}
public float getLocationLatitude() {
return locationLatitude;
}
public float getLocationLongitude() {
return locationLongitude;
}
// No need for getLocation method
}
或者我只是愚蠢(我昨天真的选择了Retrofit,Parceler和GSON)并且应该创建两个元数据类MetadataExternal
和MetadataInternal
来用于接收和发送到服务器?