Retrofit 自定义Gson对象解决日期格式问题

小咪咪 2022-07-14 02:45 296阅读 0赞

自定义Gson对象

调整json里面的一些格式,比如,Date Format。创建一个Gson 对象并把它传递给GsonConverterFactory.create()。

  1. GsonBuilder builder = new GsonBuilder();
  2. builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
  3. public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
  4. return new Date(json.getAsJsonPrimitive().getAsLong());
  5. }
  6. });
  7. Gson gson = builder.create();
  8. Retrofit retrofit = new Retrofit.Builder()
  9. .baseUrl("http://api.nuuneoi.com/base/")
  10. .addConverterFactory(GsonConverterFactory.create(gson))
  11. .build();
  12. service = retrofit.create(APIService.class);

发表评论

表情:
评论列表 (有 0 条评论,296人围观)

还没有评论,来说两句吧...

相关阅读