Skip to content

Instantly share code, notes, and snippets.

@mbobiosio
Created August 19, 2019 20:05
Show Gist options
  • Save mbobiosio/23a144475e1d872e8b5726b5c8466a27 to your computer and use it in GitHub Desktop.
Save mbobiosio/23a144475e1d872e8b5726b5c8466a27 to your computer and use it in GitHub Desktop.
public class ApiUtil {
public final static String BASE_URL = "";
private static Retrofit retrofit = null;
public static OkHttpClient getOkHttpClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return new OkHttpClient.Builder()
.addInterceptor(interceptor)
.connectTimeout(100, TimeUnit.SECONDS)
.readTimeout(100, TimeUnit.SECONDS)
.writeTimeout(100, TimeUnit.SECONDS)
.build();
}
public static Retrofit getClient() {
if (retrofit==null) {
Gson gson = new GsonBuilder().setLenient().create();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(getOkHttpClient())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
//Add these to you app build.gradle
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment