【问题标题】:android retrofit post request - exclude field from the bodyandroid改造后请求 - 从正文中排除字段
【发布时间】:2018-09-22 07:27:37
【问题描述】:

我正在编写和android添加并使用retrofit2将数据发送到服务器。

我试图从正在序列化并发送到服务器的对象中排除一个字段。

该对象被命名为“Cheer”,当被发送到服务器时,我尝试从中排除字段 id。 iv'e 尝试使用 @Expose(false, false) 并解释 here 并尝试将字段设为私有,但仍将其发送到服务器。请参阅下面的 api、对象和调用。请注意,它有效,对象已添加到服务器,唯一的问题是,id 仍以 JSON 格式发送,我需要将其排除在外。

谢谢!!!

public class Cheer {
    @Expose(deserialize = false, serialize = false)
    private int id;
}





public interface CheersAPI {

    String BASE_URL = "url:port";

    @POST("/cheers")
    Call<Cheer> AddCheer(@Body Cheer cheer);
}



cheersAPI.AddCheer(cheerToAdd).enqueue(new Callback<Cheer>(){
                            @Override
                            public void onResponse(Call<Cheer> call, Response<Cheer> response) {
                                Log.d("in the on response", "done creating a cheer");
                            }

                            @Override
                            public void onFailure(Call<Cheer> call, Throwable t) {
                                Log.d("failed", "failed to add a cheer here!");
                            }


                        });

【问题讨论】:

  • 您使用的是哪个转换器?格森?

标签: java android post retrofit retrofit2


【解决方案1】:

科特林

只需使用注解 @Transient 从请求中排除 POJO 中的任何变量

例子

data class Group(val group_id: String, 
                 val group_name: String,
                 @Transient val isChecked:Boolean = false)

瞬态文档

/**
 * Marks the JVM backing field of the annotated property as `transient`, meaning that it is not
 * part of the default serialized form of the object.
 */

【讨论】:

    【解决方案2】:

    这就是改造的方法

        val gson: Gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
    
        builder = Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    

    在你的模型中

    // These values are read as JSON objects only in server response
    @SerializedName("someField")
    @Expose(serialize = false, deserialize = true)
    var someField: String? = null
    

    例如这里,我们不会将 JSON 对象发送到服务器 (deserialize = false),但我们会接收它作为响应 (deserialize = true)

    【讨论】:

      【解决方案3】:

      访问here了解详情。

      基本上, 默认的 Gson 实例不会考虑 @Expose。为了使用它,您需要使用自定义 Gson 实例:

      GsonBuilder builder = new GsonBuilder();  
      builder.excludeFieldsWithoutExposeAnnotation();  
      Gson gson = builder.create();  
      

      但如果您这样做,则必须将 @Expose 添加到所有模型类中的每个字段,否则它们将不会被 GSON 序列化或反序列化。

      【讨论】:

        【解决方案4】:

        我假设您使用的是 Gson。你可以使用transient。

        private transient int id;

        如果您需要更复杂的解决方案,请查看Gson: How to exclude specific fields from Serialization without annotations

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-25
          • 1970-01-01
          • 1970-01-01
          • 2016-11-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多