Android - GSon + RetroFit中的继承和抽象类

时间:2015-07-09 12:18:43

标签: android gson abstract-class retrofit

我有以下类层次结构

public abstract class SyncModel {
    @Expose
    @SerializedName("id")
    private Long globalId;

    @Expose
    protected DateTime lastModified;

    /* Constructor, methods... */
}

public class Event extends SyncModel {
    @Expose
    private String title;

    /* Other fields, constructor, methods... */
}

我需要向后端发送一个Event实例。

案例1. @Body

当我在请求正文中发布Event实例时,它被序列化了。

RetroFit Java界面:

public interface EventAPI {
    @POST("/event/create")
    void sendEvent(@Body Event event, Callback<Long> cbEventId);
}

RetroFit日志:

D   Retrofit    ---> HTTP POST http://hostname:8080/event/create
D   Retrofit    Content-Type: application/json; charset=UTF-8
D   Retrofit    Content-Length: 297
D   Retrofit    {"title":"Test Event 01",...,"id":null,"lastModified":"2015-07-09T14:17:08.860+03:00"}
D   Retrofit    ---> END HTTP (297-byte body)

案例2. @Field

但是当我在请求参数中发布Event实例时,只有抽象类被序列化。

RetroFit Java界面:

@FormUrlEncoded
@POST("/event/create")
void sendEvent(@Field("event") Event event, Callback<Long> cbEventId);

RetroFit日志:

D   Retrofit    ---> HTTP POST http://hostname:8080/event/create
D   Retrofit    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
D   Retrofit    Content-Length: 101
D   Retrofit    event=SyncModel%28globalId%3Dnull%2C+lastModified%3D2015-07-09T13%3A36%3A33.510%2B03%3A00%29
D   Retrofit    ---> END HTTP (101-byte body)

注意区别。

问题

为什么?
如何将序列化的Event实例发送到请求参数的后端?
我是否需要为抽象类编写自定义JSON序列化程序? (例如:Polymorphism with JSON
或者它是RetroFit特定功能(忽略子类)?

我还注意到,在第二种情况下,globalId字段序列化名称为globalId,但它应该是id!这让我觉得RetroFit使用GsonConverter @Field而不是@Body参数...

配置

Gradle依赖

compile 'com.squareup.retrofit:retrofit:1.9.+'
compile 'com.squareup.okhttp:okhttp:2.3.+'
compile 'net.danlew:android.joda:2.8.+'
compile ('com.fatboyindustrial.gson-jodatime-serialisers:gson-jodatime-serialisers:1.1.0') {  // GSON + Joda DateTime
    exclude group: 'joda-time', module: 'joda-time'
}

REST客户端

public final class RESTClient {

    // Not a real server URL
    public static final String SERVER_URL = "http://hostname:8080";

    // one-time initialization
    private static GsonBuilder builder = new GsonBuilder()
            .serializeNulls()
            .excludeFieldsWithoutExposeAnnotation()
            .setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'");
    // Joda DateTime type support
    private static Gson gson = Converters.registerDateTime(builder).create();

    private static RestAdapter restAdapter = new RestAdapter.Builder()
            .setLogLevel(RestAdapter.LogLevel.FULL)     // for development
            .setEndpoint(SERVER_URL)
            .setConverter(new GsonConverter(gson))    // custom converter
            .build();

    private static final EventAPI eventService = restAdapter.create(EventAPI.class);
    /* + Getter for eventService */

    static {
        // forget them
        restAdapter = null;
        gson = null;
        builder = null;
    }

}

呼叫

RESTClient.getEventService().sendEvent(event, new Callback<Long>() {/* ... */});

1 个答案:

答案 0 :(得分:5)

看看@Field's documentation。它说:

  

使用String#valueOf(Object)将值转换为字符串,然后形成URL编码。

String#valueOf(Object)打电话给Object#toString()。我认为您的SyncModeltoString()方法而Event没有。当Retrofit调用String.valueOf(event)时,会调用SyncModel#toString()而不是Event#toString()。这就是您在改造日志中看不到title的原因。

在转换@Field参数时,Gson根本没有扮演任何角色。它可以是 - 您可以使toString()方法看起来像这样:

@Override
public String toString() {
    return GsonProvider.getInstance().toJson(this);
}

将它放在抽象的SyncModel类中,它也适用于Event