import java.lang.reflect.Type;
import org.apache.commons.io.IOUtils;
import retrofit.converter.*;
import retrofit.mime.*;
public class StringConverter implements Converter {
private String charset;
private static final String MIME_TYPE = “text/plain; charset=”;
public StringConverter(String charset) {
this.charset = charset;
}
public StringConverter() {
this.charset = “UTF-8”;
}
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
try{
return IOUtils.toString(body.in(), charset);
}catch(Exception e){
throw new AssertionError(e);
}
}
@Override
public TypedOutput toBody(Object object) {
try{
return new TypedByteArray(MIME_TYPE, object.toString().getBytes(charset));
}catch(Exception e){
throw new AssertionError(e);
}
}
}
답글 남기기