我正在使用Retrofit和Parceler库来与我的服务器通信。
服务器有以下两种API方法:
/api/metadata/{id}
返回以下 JSON 的GET
{
"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;
}
}
几何点:
@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;
}
}
我想在整个应用程序中使用元数据类。我想查询服务器,接收元数据,更新它,然后发送到服务器。显然,GET 和 POST 的元数据格式不同。因此,我希望在收到 GET 后将其转换为 POST 格式。
我的问题是是否有可能以某种方式声明注释,以便 Retrofit 和 Parceler 知道参数,从 JSON 反序列化它,但通过我可以将其分解为“location_latitude”和“location_longitude”的方法location
将其写入Metadata
类。setLocation()
这是所需元数据类的一些伪代码:
@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
用于接收和发送到服务器?