Last active
July 8, 2019 12:49
-
-
Save h4h13/d6fb27e81bfae6f2bf6d0c7a059a3f7b to your computer and use it in GitHub Desktop.
Retrofit Singleton class (Retrofit 2.1.0) by Using OkHttp3Client
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
import android.Manifest; | |
import android.support.annotation.RequiresPermission; | |
import android.util.Log; | |
import com.google.gson.GsonBuilder; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Locale; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.Cache; | |
import okhttp3.Interceptor; | |
import okhttp3.OkHttpClient; | |
import okhttp3.OkHttpClient.Builder; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
/** | |
* Created by MonkeyDLuffy on 9/9/2016. | |
*/ | |
public class RetrofitClient { | |
private static final String TAG = RetrofitClient.class.getSimpleName(); | |
private static String baseUrl = "YOUR_BASE_URL_HERE"; | |
private static volatile Retrofit sRetrofit = null; | |
private static RetrofitService retrofitService; | |
public RetrofitClient() { | |
} | |
public static RetrofitService getApiService() { | |
return initRetrofitService(); | |
} | |
private static RetrofitService initRetrofitService() { | |
if (retrofitService == null) { | |
synchronized (RetrofitClient.class) { | |
if (retrofitService == null) { | |
retrofitService = getRetrofit().create(RetrofitService.class); | |
} | |
} | |
} | |
return retrofitService; | |
} | |
@RequiresPermission(Manifest.permission.INTERNET) | |
private synchronized static Retrofit getRetrofit() { | |
if (sRetrofit == null) { | |
synchronized (RetrofitClient.class) { | |
if (sRetrofit == null) { | |
sRetrofit = new Retrofit.Builder() | |
.baseUrl(baseUrl) | |
.client(createClient()) | |
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create())) | |
.build(); | |
} | |
} | |
} | |
return sRetrofit; | |
} | |
private static OkHttpClient createClient() { | |
return new Builder() | |
.cache(new Cache(new File(App.getContext().getCacheDir(), "http"), 1024 * 1024 * 10)) | |
.readTimeout(30, TimeUnit.SECONDS) | |
.writeTimeout(30, TimeUnit.SECONDS) | |
.addInterceptor(createInterceptor()).build(); | |
} | |
private static Interceptor createInterceptor() { | |
return new Interceptor() { | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
Request request = chain.request(); | |
if (request.method().equals("POST")) { | |
if (Util.isNetworkAvailable()) { | |
Log.d(TAG, "intercept: Intercept connected " + request.url()); | |
request.newBuilder().header("Cache-Control", "only-if-cached").build(); | |
} else { | |
Log.d(TAG, "intercept: Intercept not connected " + request.url()); | |
request.newBuilder().header("Cache-Control", String.format(Locale.getDefault(), "public, max-stale=%d", 86400)); | |
} | |
} | |
Response response = chain.proceed(request); | |
//Re-write response CC Header to force use of cache | |
return response.newBuilder() | |
.header("Cache-Control", "public, max-age=86400") | |
.header("Pragma", "public") | |
.build(); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment