Instantly share code, notes, and snippets.
CreatedApril 22, 2015 08:04
Save plastiv/08538a095d2d35acab05 to your computer and use it in GitHub Desktop.
Retrofit multipart convertor
This file contains hidden or 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 | |
publicclassArticle { | |
Stringauthor; | |
Filephoto; | |
} |
This file contains hidden or 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 @interfaceMultipartBody { | |
} |
This file contains hidden or 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
publicclassMultipartConvertorimplementsConverter { | |
privatefinalConverterdelegateConvertor; | |
publicMultipartConvertor(ConvertergsonConverter) { | |
this.delegateConvertor =gsonConverter; | |
} | |
@OverridepublicObjectfromBody(TypedInputbody,Typetype)throwsConversionException { | |
returndelegateConvertor.fromBody(body,type); | |
} | |
@OverridepublicTypedOutputtoBody(Objectobject) { | |
Class<?>rawType =object.getClass(); | |
if (rawType.isAnnotationPresent(MultipartBody.class)) { | |
List<Field>fields =ReflectionUtil.getFields(rawType); | |
MultipartTypedOutputmultipartTypedOutput =newMultipartTypedOutput(); | |
for (Fieldfield :fields) { | |
if (field.getClass().equals(String.class)) { | |
try { | |
Stringvalue = (String)field.get(object); | |
TypedStringtypedString =newTypedString(value); | |
multipartTypedOutput.addPart(field.getName(),typedString); | |
}catch (IllegalAccessExceptione) { | |
// TODO: handle | |
} | |
}elseif (field.getClass().equals(File.class)) { | |
try { | |
Filevalue = (File)field.get(object); | |
TypedFiletypedFile =newTypedFile("todo",value); | |
multipartTypedOutput.addPart(field.getName(),typedFile); | |
}catch (IllegalAccessExceptione) { | |
// TODO: handle | |
} | |
}else { | |
thrownewUnsupportedOperationException("Not implemented: " +field.getClass()); | |
} | |
} | |
returnmultipartTypedOutput; | |
} | |
returndelegateConvertor.toBody(object); | |
} | |
} |
This file contains hidden or 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
publicinterfaceRetrofit { | |
@POST("/api/v1/articles/") | |
publicObservable<Response>createArticle(@BodyArticlearticle); | |
} |
johnjohndoe commentedApr 22, 2015
I link theStackoverflow question here.
Interesting! Maybe@JakeWharton wants to include this inRetrofit. I createdthis issue.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment