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

naX*_*aXa 13 android abstract-class gson 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... */
}
Run Code Online (Sandbox Code Playgroud)

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

情况1. @Body

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

RetroFit Java界面:

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

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);
Run Code Online (Sandbox Code Playgroud)

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序列化程序?(例如:使用JSON进行多态)
或者它是否为RetroFit特定功能(忽略子类)?

我也注意到在第二种情况下globalId字段序列化名称是globalId,但它应该是id!这让我觉得,改装使用不同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'
}
Run Code Online (Sandbox Code Playgroud)

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

呼叫

RESTClient.getEventService().sendEvent(event, new Callback<Long>() {/* ... */});
Run Code Online (Sandbox Code Playgroud)

Ego*_*uba 5

看看@Field文档.它说:

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

String#valueOf(Object)打电话给Object#toString()里面.我想你SyncModel有一个toString()方法而Event不是.当Retrofit调用时String.valueOf(event),SyncModel#toString()调用而不是Event#toString().这就是你没有title在Retrofit日志中看到的原因.

转换@Field参数时,Gson根本不扮演任何角色.它可以是 - 你可以使你的toString()方法看起来像这样:

@Override
public String toString() {
    return GsonProvider.getInstance().toJson(this);
}
Run Code Online (Sandbox Code Playgroud)

把它放在你的抽象SyncModel类中,它也应该工作Event.

  • 为了增加这一点,它很有意义,因为您可能不会将复杂对象作为查询参数发送,而是将它们作为请求体的一部分发送. (2认同)