Retrofit Android Example Tutorial

March 13, 2018 1 Comments

Retrofit Android


Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services. We’ll not go into the details of Retrofit 1.x versions and jump onto Retrofit 2 directly which has a lot of new features and a changed internal API compared to the previous versions.

Retrofit automatically serialises the JSON response using a POJO(Plain Old Java Object) which must be defined in advanced for the JSON Structure. To serialise JSON we need a converter to convert it into Gsonfirst. We need to add the following dependencies in our build.grade file.

Let's Start :
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

OkHttp dependency is already shipped with Retrofit 2 dependency. If you wish to use a separate OkHttp dependency, you should exclude the OkHttp dependency from Retrofit 2 as:
compile ('com.squareup.retrofit2:retrofit:2.1.0') {  
// exclude Retrofit’s OkHttp dependency module and define your own module import
exclude module: 'okhttp'
}
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'


  • The logging-interceptor generates a log string of the entire response that’s returned.

  • There are other converters to parse the JSON to the necessary type. A few of them are listed below.



  1. Jackson : com.squareup.retrofit2:converter-jackson:2.1.0

  2. Moshi : com.squareup.retrofit2:converter-moshi:2.1.0

  3. Protobuf : com.squareup.retrofit2:converter-protobuf:2.1.0

  4. Wire : com.squareup.retrofit2:converter-wire:2.1.0

  5. Simple XML : com.squareup.retrofit2:converter-simplexml:2.1.0


OkHttp Interceptors


Interceptors are a powerful mechanism present in OkHttp that can monitor, rewrite, and retry calls.
Interceptors can be majorly divided into two categories:

  • Application Interceptors : To register an application interceptor, we need to call addInterceptor()on OkHttpClient.Builder

  • Network Interceptors : To register a Network Interceptor, invoke addNetworkInterceptor() instead of addInterceptor()


Setting Up the Retrofit Interface


package com.droidquery.retrofitintro;

import com.droidquery.retrofitintro.pojo.MultipleResource;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

class APIClient {

private static Retrofit retrofit = null;

static Retrofit getClient() {

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();


retrofit = new Retrofit.Builder()
.baseUrl("https://reqres.in")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();



return retrofit;
}

}

The getClient() method in the above code will be called every time while setting up a Retrofit interface. Retrofit provides with a list of annotations for each of the HTTP methods: @GET, @POST, @PUT, @DELETE, @PATCH or @HEAD.

Let’s see how our APIInterface.java class looks like.
package com.droidquery.retrofitintro;

import com.droidquery.retrofitintro.pojo.MultipleResource;
import com.droidquery.retrofitintro.pojo.User;
import com.droidquery.retrofitintro.pojo.UserList;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

interface APIInterface {

@GET("/api/unknown")
Call doGetListResources();

@POST("/api/users")
Call createUser(@Body User user);

@GET("/api/users?")
Call doGetUserList(@Query("page") String page);

@FormUrlEncoded
@POST("/api/users?")
Call doCreateUserWithField(@Field("name") String name, @Field("job") String job);
}

In the above class, we’ve defined some methods that perform HTTP requests with annotation.

more : Handle Multiple Response in Rectrofit click to below link

Read more related to Rectrofit :

 

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

1 comments:

Hot-product

Best Watch Store

Popular Posts

Contact Form

Name

Email *

Message *