kks*_*ksi 1 android realm gson retrofit
我试图将RealmObject用作Retrofit 2中的PUT消息正文内容。我已经使用JsonSerializer实现了自定义Gson,它在Retrofit之外运行良好,但是我仍然没有在请求正文中获取对象数据。
用于改造和gson的Gradle构建:
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Run Code Online (Sandbox Code Playgroud)
改造服务:
public interface LogstashRetrofitService {
@PUT(LOGSTASH_SERVER_PATH)
Call<ResponseBody> putLogstashMessage(@Body LogstashMessage logstashMessage);
}
Run Code Online (Sandbox Code Playgroud)
建筑改造:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(LOGSTASH_SERVER_HOST)
.addConverterFactory(GsonConverterFactory.create(RealmObjectGsonBuilder.getRealmGson()))
.client(httpClient.build())
.build();
logstashRetrofitService = retrofit.create(LogstashRetrofitService.class);
logstashRetrofitService.putLogstashMessage(logstashMessage).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {}
});
Run Code Online (Sandbox Code Playgroud)
这是因为GSON尝试使用反射基于字段对您的对象进行序列化,但是Realm实例数据只能通过代理getter setter方法进行访问。
请参阅相关的未解决问题,因为GSON并不十分在乎,因此没有配置使用getters / setters代替https://github.com/google/gson/issues/232
使用Jackson或LoganSquare json解析器可以做到这一点。
或者,您需要制作RealmObject的非托管副本,可以通过这样做realmObject.copyFromRealm()来创建与Realm分离的类的深层副本。
| 归档时间: |
|
| 查看次数: |
944 次 |
| 最近记录: |