Skip to content

Instantly share code, notes, and snippets.

@thanhnh98
Created January 9, 2020 03:23
Show Gist options
  • Save thanhnh98/3e79dd403a622b83d260c344d5019d21 to your computer and use it in GitHub Desktop.
Save thanhnh98/3e79dd403a622b83d260c344d5019d21 to your computer and use it in GitHub Desktop.
package com.thanh.mvp_architecture.network;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* <p/>
* This is the main entry point for network communication. Use this class for instancing REST services which do the
* actual communication.
*/
public class RestClient {
private static final String BASE_URL = "http://www.mocky.io/";
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create()).client(getHttpClient().build());
public static OkHttpClient.Builder getHttpClient() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
return httpClient;
}
private static Retrofit retrofit = builder.build();
public static <S> S createService(Class<S> serviceClass) {
return retrofit.create(serviceClass);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment