Skip to content

Instantly share code, notes, and snippets.

@pflammertsma
Created January 18, 2016 17:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pflammertsma/b9cb0c4688bbd335ab66 to your computer and use it in GitHub Desktop.
Save pflammertsma/b9cb0c4688bbd335ab66 to your computer and use it in GitHub Desktop.
Retrofit converter that outputs "text/plain" for primary types
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;
}
}
@pflammertsma
Copy link
Author

To use this, make sure this converter appears first among the converters when constructing Retrofit. For instance:

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BuildConfig.API)
            .addConverterFactory(PrimitiveConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(getGson()))
            .client(client)
            .build();

@PortionLK
Copy link

Error:(32, 5) error: method does not override or implement a method from a supertype (at line 34)

@Yazon2006
Copy link

@jcodesdotme I have same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment