refactor(NullOnEmptyConverterFactory.java): 使用 Kotlin 重写
This commit is contained in:
parent
88985dff4a
commit
b8ad4cbff6
|
|
@ -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);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue