Skip to content

Instantly share code, notes, and snippets.

@mankum93
Created August 15, 2019 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mankum93/179c2d5378f27e95742c3f2434de7168 to your computer and use it in GitHub Desktop.
Save mankum93/179c2d5378f27e95742c3f2434de7168 to your computer and use it in GitHub Desktop.
We enable filtering the request/response params for Retrofit using a HttpLoggingInterceptor
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public final class NetworkingClient {
private static final String TAG = NetworkingClient.class.getSimpleName();
private static Retrofit sRetrofitInstance;
// Request patterns to filter
private static final String[] REQUEST_PATTERNS = {"Content-Type", };
// Response patterns to filter
private static final String[] RESPONSE_PATTERNS = {"Server", "server", "X-Powered-By", "Set-Cookie", "Expires", "Cache-Control", "Pragma", "Content-Length", "access-control-allow-origin"};
public static synchronized Retrofit getRetrofitClient() {
if (sRetrofitInstance ==null) {
// Log requests and response
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
// Blacklist the elements not required
for(String pattern : REQUEST_PATTERNS){
if(message.startsWith(pattern)){
return;
}
}
// Any response patterns as well...
for(String pattern : RESPONSE_PATTERNS){
if(message.startsWith(pattern)){
return;
}
}
Log.d("RETROFIT", message);
}
});
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();
sRetrofitInstance = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return sRetrofitInstance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment