Last active
June 30, 2019 17:07
-
-
Save ImaginativeShohag/99827a5a9d33fbf37083b8e7bc379203 to your computer and use it in GitHub Desktop.
Retrofit 2 Api Client Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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