Created
February 1, 2018 05:24
-
-
Save rhari991/64a9d404d8ad69ede0ca6f2c6e6f41c3 to your computer and use it in GitHub Desktop.
An example of a custom response converter for Retrofit
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
class PasteListConverter implements Converter<ResponseBody, List<Paste>> { | |
private final Serializer serializer; | |
PasteListConverter() { | |
serializer = new Persister(new AnnotationStrategy()); | |
} | |
@Override | |
public List<Paste> convert(ResponseBody value) throws IOException { | |
String originalResponse = value.string(); | |
String correctedResponse = "<pastes>\r\n" + originalResponse + "\r\n</pastes>"; | |
try { | |
PasteGroup pasteGroup = serializer.read(PasteGroup.class, correctedResponse); | |
return pasteGroup.getPasteList(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
@Root(name = "pastes", strict = false) | |
private static class PasteGroup { | |
@ElementList(name = "pastes", inline = true, required = false) | |
private List<Paste> pasteList; | |
PasteGroup(@ElementList(name = "pastes", inline = true, required = false) List<Paste> pasteList) { | |
this.pasteList = pasteList; | |
} | |
List<Paste> getPasteList() { | |
return pasteList; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment