Created
April 22, 2015 08:04
-
-
Save plastiv/08538a095d2d35acab05 to your computer and use it in GitHub Desktop.
Retrofit multipart convertor
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
@MultipartBody | |
public class Article { | |
String author; | |
File photo; | |
} |
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
@Target(TYPE) | |
@Retention(RUNTIME) | |
public @interface MultipartBody { | |
} |
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
public class MultipartConvertor implements Converter { | |
private final Converter delegateConvertor; | |
public MultipartConvertor(Converter gsonConverter) { | |
this.delegateConvertor = gsonConverter; | |
} | |
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException { | |
return delegateConvertor.fromBody(body, type); | |
} | |
@Override public TypedOutput toBody(Object object) { | |
Class<?> rawType = object.getClass(); | |
if (rawType.isAnnotationPresent(MultipartBody.class)) { | |
List<Field> fields = ReflectionUtil.getFields(rawType); | |
MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput(); | |
for (Field field : fields) { | |
if (field.getClass().equals(String.class)) { | |
try { | |
String value = (String) field.get(object); | |
TypedString typedString = new TypedString(value); | |
multipartTypedOutput.addPart(field.getName(), typedString); | |
} catch (IllegalAccessException e) { | |
// TODO: handle | |
} | |
} else if (field.getClass().equals(File.class)) { | |
try { | |
File value = (File) field.get(object); | |
TypedFile typedFile = new TypedFile("todo", value); | |
multipartTypedOutput.addPart(field.getName(), typedFile); | |
} catch (IllegalAccessException e) { | |
// TODO: handle | |
} | |
}else { | |
throw new UnsupportedOperationException("Not implemented: " + field.getClass()); | |
} | |
} | |
return multipartTypedOutput; | |
} | |
return delegateConvertor.toBody(object); | |
} | |
} |
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
public interface Retrofit { | |
@POST("/api/v1/articles/") | |
public Observable<Response> createArticle(@Body Article article); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I link the Stackoverflow question here.
Interesting! Maybe @JakeWharton wants to include this in Retrofit. I created this issue.