我正在使用
改造2
我被困在这个问题上了。我为body编写了简单的实体:
data class DateRequest(
@JsonAdapter(RetrofitDateSerializer::class)
@SerializedName("date") @Expose val date: OffsetDateTime)
我还为它编写了自定义序列化程序:
class RetrofitDateSerializer : JsonSerializer<OffsetDateTime> {
override fun serialize(
srcDate: OffsetDateTime?,
typeOfSrc: Type?,
context: JsonSerializationContext?
): JsonElement? {
val formatted = DateTimeUtil.convertFromDateTime(srcDate!!)
return JsonPrimitive(formatted)
}}
DateTimeUtil
:
fun convertFromDateTime(dateTime: OffsetDateTime): String {
val formatter = formatDateTime()
return formatter.format(dateTime)
}
fun formatDateTime(): DateTimeFormatter {
return DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss").withLocale(Locale.US)
}
)]}'{"date": "2018-12-07 06:00:00"}
“这是怎么回事?”
)]}'
“可以挂在我的手机前面”
日期
“请求中的json?
@POST("changecleaning/{userId}/{cleaningId}/{status}")
fun changeCleaning(
@Path("userId") userId: Long,
@Path("cleaningId") cleaningId: Long,
@Path("status") status: Int,
@Body date: DateRequest
): Maybe<Status>
只有我找到的才是我想要的
JsonWriter
做一些魔术
buffer.readByteString()
它储存破碎的尸体。
GSONB转换器
:
@Override public RequestBody convert(T value) throws IOException {
Buffer buffer = new Buffer();
Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
JsonWriter jsonWriter = gson.newJsonWriter(writer);
adapter.write(jsonWriter, value);
jsonWriter.close();
return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}