写在前面:前几天做项目用到retrofit来做网络请求,接触这个框架也没多久,就按照网站上的路子走,毕竟农村来的,网上的一些套路…–!,然后就掉坑里了…(ps:我们后台接口需要的参数只能少传递不能多传递,坑)
开始是这样写的:
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.serializeNulls()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(base_host_url)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
.build();
这样就导致如果传递对象的话,对象里面的所有字段如果没有赋值的话就会为null并且传递给服务器,我们服务器,咳咳,不给ignore,所以我就找啊找啊,到底什么鬼为什么空字段转成json的时候会有呢,大大大大最后在这了,看这里:
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.serializeNulls()//就是我让那些空字段保留着的!!!!!!
.create();
那么大的serializeNulls愣是一直没看到,还是得走走❤呐!菜鸟的路还很长很长…