android 继承和抽象方法,Android – 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... */

}

我需要发送一个事件实例到后端.

案例1. @Body

当我在一个请求体中发布Event实例时,它被序列化.

RetroFit Java接口:

public interface EventAPI {

@POST("/event/create")

void sendEvent(@Body Event event, Callback 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 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)

注意区别.

问题

为什么?

如何在请求参数中将序列化的事件实例发送到后端?

我需要为抽象类编写一个自定义的JSON序列化程序吗? (例如:Polymorphism with JSON)

还是它是一个RetroFit的具体功能(忽略小孩类)?

我也注意到,在第二种情况下,globalId字段序列化名称是globalId,但它应该是id!它使我认为RetroFit对于@Field使用不同的GsonConverter而不是@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() {/* ... */});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值