Instantly share code, notes, and snippets.
Save flyfire/8719cd56747ea1824b5743e1cf9ba618 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); | |
} |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment