Created
March 18, 2016 07:29
-
-
Save snadjafi/da00c77e5535d4977330 to your computer and use it in GitHub Desktop.
latest to string converter factory for retrofit 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 class ToStringConverterFactory extends Converter.Factory { | |
private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain"); | |
@Override | |
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { | |
if (String.class.equals(type)) { | |
return new Converter<ResponseBody, String>() { | |
@Override | |
public String convert(ResponseBody value) throws IOException { | |
return value.string(); | |
} | |
}; | |
} | |
return null; | |
} | |
@Override | |
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { | |
if (String.class.equals(type)) { | |
return new Converter<String, RequestBody>() { | |
@Override | |
public RequestBody convert(String value) throws IOException { | |
return RequestBody.create(MEDIA_TYPE, value); | |
} | |
}; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment