Skip to content

Instantly share code, notes, and snippets.

@ImaginativeShohag
Last active June 30, 2019 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ImaginativeShohag/99827a5a9d33fbf37083b8e7bc379203 to your computer and use it in GitHub Desktop.
Save ImaginativeShohag/99827a5a9d33fbf37083b8e7bc379203 to your computer and use it in GitHub Desktop.
Retrofit 2 Api Client Class
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
private static Retrofit retrofit = null;
private static OkHttpClient buildClient() {
return new OkHttpClient
.Builder()
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
}
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.client(buildClient())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://xyz.xyz")
.build();
}
return retrofit;
}
}
/*
Inside "build.gradle" file:
dependencies {
// ...
// ...
// ...
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment