Created
September 28, 2016 17:23
-
-
Save tylerjroach/23a363cc3dcd11108ef25e0b7abbab82 to your computer and use it in GitHub Desktop.
Retrofit Singleton Example
This file contains hidden or 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
package com.tylerjroach.example.retrofit2; | |
import com.tylerjroach.example.BuildConfig; | |
import com.tylerjroach.example.util.Constants; | |
import okhttp3.OkHttpClient; | |
import okhttp3.logging.HttpLoggingInterceptor; | |
import retrofit.RestAdapter; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
public class Retrofit2Client { | |
private static Retrofit2Client instance = null; | |
private Retrofit retrofit; | |
private OkHttpClient client; | |
private ExploreService exploreService; | |
public Retrofit2Client() { | |
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); | |
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | |
OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder(); | |
okHttpBuilder.addInterceptor(new TokenInterceptor()); | |
if (BuildConfig.DEBUG) { | |
okHttpBuilder.addInterceptor(loggingInterceptor); | |
} | |
client = okHttpBuilder.build(); | |
retrofit = new Retrofit.Builder().baseUrl(Constants.API_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.client(client) | |
.build(); | |
exploreService = retrofit.create(ExploreService.class); | |
} | |
public static Retrofit2Client getInstance() { | |
if (instance == null) { | |
instance = new Retrofit2Client(); | |
} | |
return instance; | |
} | |
public ExploreService getExploreService() { | |
return exploreService; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you