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

    xiaoxiao2021-12-12  8

    自定义Gson对象

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

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

    转载请注明原文地址: https://ju.6miu.com/read-900212.html

    最新回复(0)