Created
January 18, 2016 17:15
-
-
Save pflammertsma/b9cb0c4688bbd335ab66 to your computer and use it in GitHub Desktop.
Retrofit converter that outputs "text/plain" for primary types
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
package com.pixplicity.retrofit; | |
import com.squareup.okhttp.MediaType; | |
import com.squareup.okhttp.RequestBody; | |
import java.io.IOException; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
import retrofit.Converter; | |
/** | |
* Outputs "text/plain" for primary types. This can be useful when parameters are provided in | |
* "multipart/form-data", using the @Multipart annotation. | |
* <p> | |
* This solution was adapted from: | |
* https://github.com/square/retrofit/issues/1210#issuecomment-149341629 | |
* </p> | |
*/ | |
public class PrimitiveConverterFactory extends Converter.Factory { | |
private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain"); | |
private static PrimitiveConverterFactory sInstance; | |
public static Converter.Factory create() { | |
if (sInstance == null) { | |
sInstance = new PrimitiveConverterFactory(); | |
} | |
return sInstance; | |
} | |
@Override | |
public Converter<?, RequestBody> toRequestBody(Type type, Annotation[] annotations) { | |
if (type instanceof Class && ((Class<?>) type).isEnum()) { | |
return new Converter<Enum, RequestBody>() { | |
@Override | |
public RequestBody convert(Enum value) throws IOException { | |
return RequestBody.create(MEDIA_TYPE, value.toString()); | |
} | |
}; | |
} else if (Integer.class.equals(type) || int.class.equals(type)) { | |
return new Converter<Integer, RequestBody>() { | |
@Override | |
public RequestBody convert(Integer value) throws IOException { | |
return RequestBody.create(MEDIA_TYPE, Integer.toString(value)); | |
} | |
}; | |
} else if (Boolean.class.equals(type) || boolean.class.equals(type)) { | |
return new Converter<Boolean, RequestBody>() { | |
@Override | |
public RequestBody convert(Boolean value) throws IOException { | |
return RequestBody.create(MEDIA_TYPE, Boolean.toString(value)); | |
} | |
}; | |
} else if (Byte.class.equals(type) || byte.class.equals(type)) { | |
return new Converter<Byte, RequestBody>() { | |
@Override | |
public RequestBody convert(Byte value) throws IOException { | |
return RequestBody.create(MEDIA_TYPE, Byte.toString(value)); | |
} | |
}; | |
} else if (Character.class.equals(type) || char.class.equals(type)) { | |
return new Converter<Character, RequestBody>() { | |
@Override | |
public RequestBody convert(Character value) throws IOException { | |
return RequestBody.create(MEDIA_TYPE, Character.toString(value)); | |
} | |
}; | |
} else if (Long.class.equals(type) || long.class.equals(type)) { | |
return new Converter<Long, RequestBody>() { | |
@Override | |
public RequestBody convert(Long value) throws IOException { | |
return RequestBody.create(MEDIA_TYPE, Long.toString(value)); | |
} | |
}; | |
} else if (Float.class.equals(type) || float.class.equals(type)) { | |
return new Converter<Float, RequestBody>() { | |
@Override | |
public RequestBody convert(Float value) throws IOException { | |
return RequestBody.create(MEDIA_TYPE, Float.toString(value)); | |
} | |
}; | |
} else if (Double.class.equals(type) || double.class.equals(type)) { | |
return new Converter<Double, RequestBody>() { | |
@Override | |
public RequestBody convert(Double value) throws IOException { | |
return RequestBody.create(MEDIA_TYPE, Double.toString(value)); | |
} | |
}; | |
} else if (Short.class.equals(type) || short.class.equals(type)) { | |
return new Converter<Short, RequestBody>() { | |
@Override | |
public RequestBody convert(Short value) throws IOException { | |
return RequestBody.create(MEDIA_TYPE, Short.toString(value)); | |
} | |
}; | |
} else if (String.class.equals(type)) { | |
return new Converter<String, RequestBody>() { | |
@Override | |
public RequestBody convert(String value) throws IOException { | |
return RequestBody.create(MEDIA_TYPE, value); | |
} | |
}; | |
} else if (Void.class.equals(type) || void.class.equals(type)) { | |
return new Converter<Void, RequestBody>() { | |
@Override | |
public RequestBody convert(Void value) throws IOException { | |
return null; | |
} | |
}; | |
} | |
return null; | |
} | |
} |
Error:(32, 5) error: method does not override or implement a method from a supertype (at line 34)
@jcodesdotme I have same issue.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use this, make sure this converter appears first among the converters when constructing Retrofit. For instance: