小编kks*_*ksi的帖子

将Realm对象作为Retrofit 2 PUT请求主体传递

我试图将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)

android realm gson retrofit

1
推荐指数
1
解决办法
944
查看次数

标签 统计

android ×1

gson ×1

realm ×1

retrofit ×1