-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToStringConverterFactory.java
46 lines (37 loc) · 1.44 KB
/
ToStringConverterFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.example.nguyenhuutinh.myapplication;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
public final class ToStringConverterFactory extends Converter.Factory {
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
//noinspection EqualsBetweenInconvertibleTypes
if (String.class.equals(type)) {
return new Converter<ResponseBody, Object>() {
@Override
public Object convert(ResponseBody responseBody) throws IOException {
return responseBody.string();
}
};
}
return null;
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
//noinspection EqualsBetweenInconvertibleTypes
if (String.class.equals(type)) {
return new Converter<String, RequestBody>() {
@Override
public RequestBody convert(String value) throws IOException {
return RequestBody.create(MediaType.parse("text/plain"), value);
}
};
}
return null;
}
}