Retrofit Technology Overview by Cumulations Technologies

25
Retrofit * Released by Square. * Retrofit turns your REST API into a Java interface. * Offers very easy to use REST API Solution.

Transcript of Retrofit Technology Overview by Cumulations Technologies

Page 1: Retrofit Technology Overview by Cumulations Technologies

Retrofit

* Released by Square.

* Retrofit turns your REST API into a Java interface.

* Offers very easy to use REST API Solution.

Page 2: Retrofit Technology Overview by Cumulations Technologies

Retrofit Performance Analysis

Page 3: Retrofit Technology Overview by Cumulations Technologies

Retrofit uses these library

lOkHttp

lGSON

lRxJava

Page 4: Retrofit Technology Overview by Cumulations Technologies

GSONlConversion 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

Page 5: Retrofit Technology Overview by Cumulations Technologies

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{"id":1,"name":"testName","parent":2}

Page 6: Retrofit Technology Overview by Cumulations Technologies

GSON Implementation

Page 7: Retrofit Technology Overview by Cumulations Technologies

GSON Serializationclass 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) {result.add("parent", new JsonPrimitive(parent.getId())); } return result; } }

Page 8: Retrofit Technology Overview by Cumulations Technologies

RxJava

Page 9: Retrofit Technology Overview by Cumulations Technologies

Reactive Programming

Page 10: Retrofit Technology Overview by Cumulations Technologies

Reactive Programming

Page 11: Retrofit Technology Overview by Cumulations Technologies

Reactive Programming

Page 12: Retrofit Technology Overview by Cumulations Technologies

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.

Page 13: Retrofit Technology Overview by Cumulations Technologies

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);List<Person> temp = service.listPerson("xyz");

Page 14: Retrofit Technology Overview by Cumulations Technologies

Interface's MethodslA 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

Page 15: Retrofit Technology Overview by Cumulations Technologies

Interface's MethodslPublic interface TestInterface{@GET("/users/list")Photo userList(); //Synchronous call

@POST("/users/list")void userList(@Body User ,Callback<TResponse> cb);//Asynchronous call

@GET("/users/list")Observable<TResponse> userList();//Observablel}

Page 16: Retrofit Technology Overview by Cumulations Technologies

HEADER MANIPULATIONlSet static headers for a method usingl@Headers("Cache-Control: max-age=640000")

lUse when need to send same header for all api-lRequestInterceptor requestInterceptor = new RequestInterceptor() {l @Overridel public void intercept(RequestFacade request) {l request.addHeader("User-Agent", "Retrofit-Sample-App");l }l};

Page 17: Retrofit Technology Overview by Cumulations Technologies

Retrofit Implementation

* Retrofit uses Gson by default to convert HTTP bodies to and from JSON.

* Custom GSON Converter ExampleIf you want to specify behavior that is different from Gson's defaults.

GsonBuilder gson = new GsonBuilder();gson.registerTypeAdapter(MyType2.class, new MyTypeAdapter());

Page 18: Retrofit Technology Overview by Cumulations Technologies

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)) .build();

Page 19: Retrofit Technology Overview by Cumulations Technologies

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 deserialization of objects.

Page 20: Retrofit Technology Overview by Cumulations Technologies

Retrofit Implementation

* RestAdapter.Builder() .setEndpoint(“http://www.githubservice.com”).setClient(new OkClient(okHttpClientWithCache())).setConverter(new GsonConverter(new GsonBuilder().registerTypeAdapter(Test.class, new TestDeserializer()) .build() .create(TestInterface.class);

Page 21: Retrofit Technology Overview by Cumulations Technologies

Retrofit Implementation* Use http://www.jsonschema2pojo.org/ to create POJO classes. Lets take this example-{ "weather":[ { "id":711, "main":"Smoke", "description":"smoke", "icon":"50n" }]}

Page 22: Retrofit Technology Overview by Cumulations Technologies

Retrofit Implementation

Page 23: Retrofit Technology Overview by Cumulations Technologies

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);

Page 24: Retrofit Technology Overview by Cumulations Technologies

THANK YOU

l

Page 25: Retrofit Technology Overview by Cumulations Technologies

Contact Cumulations

Are you looking for a mobile apps development team who can understand your business requirement & develop a user-centric apps?

Email: [email protected] us at: http://www.cumulations.com/

Let’s Talk