This power point presentation explains retrofit technology, it includes performance analysis, The libraries support, Implementation, serialization, Interfaces, Interface methods, header manipulation. You can learn more about retrofit implementation.
4. GSON
lConversion of an object to json or from json to object.
lAll the fields by default are included in conversions even
private fields
lIf don’t want to include the field in conversion, use
transient modifier for that field
5. GSON Implementation
* This is a class for Person-
public class Person {
public String id;
@SerializedName(user_name)
public String name;
public Person parent;
//setter getter here
}
Obtains result like this
7. GSON Serialization
class PersonSerializer implements
JsonSerializer<Person> {
public JsonElement serialize(final Person person, Type
type,JsonSerializationContext context) {
JsonObject result = new JsonObject();
result.add("id", new JsonPrimitive(person.getId()));
result.add("name", new
JsonPrimitive(person.getName()));
Person parent = person.getParent();
if (parent != null) {
12. Retrofit
1-Create Interface for API and declare methods for each
REST Call
2-Specify method type Using Annotations-
@GET,@POST,@PUT
3-For paramenters use -
@Path,@Query,@Body
4-Use RestAdapter to build the service client.
13. Retrofit Interface
public interface TestService {
@GET("/users/{user}/person")
List<Person> listPerson(@Path("user") String user);
}
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.test.com")
.build();
TestService service =
restAdapter.create(TestService.class);
14. Interface's Methods
lA method with a return type will be executed
synchronously.
lAsynchronous execution requires the last parameter of the
method be a Callback.
l Using RxJava we can have return type as Observable
16. HEADER MANIPULATION
lSet static headers for a method using
l@Headers("Cache-Control: max-age=640000")
lUse when need to send same header for all api-
lRequestInterceptor requestInterceptor = new
RequestInterceptor() {
l @Override
l public void intercept(RequestFacade request) {
l request.addHeader("User-Agent", "Retrofit-Sample-
App");
17. Retrofit Implementation
* Retrofit uses Gson by default to convert HTTP bodies to
and from JSON.
* Custom GSON Converter Example
If you want to specify behavior that is different from
Gson's defaults.
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(MyType2.class, new
MyTypeAdapter());
18. Retrofit Implementation
* Create gson object this way-
Gson gson = new
GsonBuilder().registerTypeAdapter(Person.class, new
PersonSerializer())
.create();
* Pass this object to .setConverter
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.github.com")
.setConverter(new GsonConverter(gson))
19. RestAdapter
*Using RestAdapter Object we initialize Interface
reference or service client.
* It has static class RestAdapter.Builder.
methods inside RestAdapter.Builder class-
*setEndpoint-API endpoint URL
*setClient-The HTTP client used for requests
*setRequestInterceptor-for adding Header/data to every
request.
setConverter-converter used for serialization and
23. Implementaion
* It Shows how to use SimpleXMLConverter to
communicate with an API that uses XML
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.soundcloud.com")
.setConverter(new SimpleXMLConverter())
.build();
TestInterrface service =
restAdapter.create(TestInterface.class);
25. Contact Cumulations
Are you looking for a mobile apps development team who can understand your business
requirement & develop a user-centric apps?
Email: sales@cumulations.com
Visit us at: http://www.cumulations.com/
Let’s Talk