refactor(NullOnEmptyConverterFactory.java): 使用 Kotlin 重写

This commit is contained in:
GoFly233 2020-08-13 18:56:56 +08:00 committed by HuanChengFly
parent 88985dff4a
commit b8ad4cbff6
2 changed files with 16 additions and 19 deletions

View File

@ -1,19 +0,0 @@
package com.huanchengfly.tieba.api.retrofit;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
public class NullOnEmptyConverterFactory extends Converter.Factory {
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
final Converter<ResponseBody, ?> delegate = retrofit.nextResponseBodyConverter(this, type, annotations);
return (Converter<ResponseBody, Object>) body -> {
if (body.contentLength() == 0) return null;
return delegate.convert(body);
};
}
}

View File

@ -0,0 +1,16 @@
package com.huanchengfly.tieba.api.retrofit
import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type
class NullOnEmptyConverterFactory : Converter.Factory() {
override fun responseBodyConverter(type: Type, annotations: Array<Annotation>, retrofit: Retrofit): Converter<ResponseBody, *>? {
val delegate: Converter<ResponseBody, *> = retrofit.nextResponseBodyConverter<Any>(this, type, annotations)
return (Converter<ResponseBody, Any?> { body: ResponseBody ->
if (body.contentLength() == 0L) return@Converter null
delegate.convert(body)
})
}
}