VTUFORUM
Retrofit 2.0 for Rest API call - Printable Version

+- VTUFORUM (http://www.vtuforum.com)
+-- Forum: KnowledgeBase (/Forum-KnowledgeBase)
+--- Forum: Concepts and Tutorials (/Forum-Concepts-and-Tutorials)
+--- Thread: Retrofit 2.0 for Rest API call (/Thread-Retrofit-2-0-for-Rest-API-call)



Retrofit 2.0 for Rest API call - Sandeep - 03-10-2016 08:40 PM

Hello friends,

Taking time out to contribute to forum again with some articles. So today it will be making a Rest API call from Android App using Retrofit version 2.0 which was in beta from long time

Most of the App which we develop now usually has to backend web server providing web services. We can call them and get the data, there are many libraries to do this like volley OKhttp. In this post we are taking about Retrofit because I like it very much and know little more.

Okay let’s get started,

First put the depend cines in Gradle, this is very important, because there are lot of confusion with the version number as Retrofit just came from 1, 1,9 to 2 and many versions in Okhttp. So just put the following 3 dependencies in App module level gradle

Quote:compile 'com.google.code.gson:gson:2.6.1'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.okhttp3:okhttp:3.1.2'

Its very difficult to explain the whole Retrofit in a single post, if you don't know anything about retrofit please refer the square official site to get a glimpse. Please refer the link before you read furthur. In this post, am going to use those component with added headers. Usually when we make a first contact with server, server expect the Authorization API, please try to use some free services like openweather or BollywoodAPI. So we can hit their endpoint only if we have a valid API key, so this tutorial will hit such an endpoint which need the API key.

Like every time with Retrofit, you need to write an Service interface with the required commands, one request entity and one response entity. As promise we have to add header for this request for which we have to write a Interceptor


First write an interface

Quote:public interface MyServiceAPI{
@GET("/v1/endpoint")
public Call<TempModel> getTempValues();
}

Now create a request call and all the Interceptor..

Quote: public static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create());

public static <S> S createService(Class<S> serviceClass) {
final String basic =
"Basic " + API_KEY;

httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();

Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", basic)
.header("Accept", "application/json")
.method(original.method(), original.body());

Request request = requestBuilder.build();
LOG.d("Basicvalue " + basic);
return chain.proceed(request);
}
});

OkHttpClient client = httpClient.build();
Retrofit retrofit = builder.client(client).build();
return retrofit.create(serviceClass);

Put above code in a function which will return your Model object, in my case it is TempModel like

Quote:TempModel callingAboveFunction(Class serviceClass)

Now call this function when you are calling with the serviceAPI

Quote:TempModel tempModel = Service.callingAboveFunction(MyServiceAPI.class);

So now Request is ready, call the function present in interface

Quote:Call<TempModel> temp = tempModel.getTempValues();

Prepare the response like

Quote:call.enqueue(new Callback<TempModel>() {
@Override
public void onResponse(Call<TempModel> call, Response<TempModel> response) {
if (response != null) {
Log.d("Result", response.body().toString());
}
}

@Override
public void onFailure(Call<TempModel> call, Throwable t) {

}
});


Thank you all Big Grin Big Grin

Reference: https://futurestud.io/blog/tag/retrofit