use of okhttp3.logging.HttpLoggingInterceptor in project bilibili-android-client by HotBitmapGG.
the class RetrofitHelper method initOkHttpClient.
/**
* 初始化OKHttpClient,设置缓存,设置超时时间,设置打印日志,设置UA拦截器
*/
private static void initOkHttpClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
if (mOkHttpClient == null) {
synchronized (RetrofitHelper.class) {
if (mOkHttpClient == null) {
//设置Http缓存
Cache cache = new Cache(new File(BilibiliApp.getInstance().getCacheDir(), "HttpCache"), 1024 * 1024 * 10);
mOkHttpClient = new OkHttpClient.Builder().cache(cache).addInterceptor(interceptor).addNetworkInterceptor(new CacheInterceptor()).addNetworkInterceptor(new StethoInterceptor()).retryOnConnectionFailure(true).connectTimeout(30, TimeUnit.SECONDS).writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).addInterceptor(new UserAgentInterceptor()).build();
}
}
}
}
use of okhttp3.logging.HttpLoggingInterceptor in project coins-android by bubelov.
the class MainModule method httpClient.
@Provides
@Singleton
OkHttpClient httpClient() {
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS);
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
httpClientBuilder.addInterceptor(loggingInterceptor);
httpClientBuilder.addNetworkInterceptor(new StethoInterceptor());
}
return httpClientBuilder.build();
}
use of okhttp3.logging.HttpLoggingInterceptor in project iNGAGE by davis123123.
the class UserRecentCommentHandler method enqueue.
// get all recent comments for each room, for the user
public void enqueue(String username) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder().client(httpClient.build()).addConverterFactory(GsonConverterFactory.create()).baseUrl(get_url).build();
Interface service = retrofit.create(Interface.class);
Call<ResponseBody> call = service.get(username);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.i("STATE", "Retrofit response code: " + response.code());
if (response.isSuccessful()) {
Log.i("STATE", "Retrofit POST Success");
try {
serverResponse = response.body().string();
createCommentsList(serverResponse);
callBackData.notifyChange();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.i("Retrofit Error Code:", String.valueOf(response.code()));
Log.i("Retrofit Error Body", response.errorBody().toString());
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.i("STATE", "Retrofit Failure");
}
});
}
use of okhttp3.logging.HttpLoggingInterceptor in project instructure-android by instructure.
the class CanvasRestAdapter method buildRollCallAdapter.
public Retrofit buildRollCallAdapter(@NonNull String url) {
final Gson gson = new GsonBuilder().setLenient().create();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(DEBUG ? HttpLoggingInterceptor.Level.HEADERS : HttpLoggingInterceptor.Level.NONE);
return new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create(gson)).client(new OkHttpClient.Builder().addInterceptor(loggingInterceptor).addInterceptor(new RollCallInterceptor()).readTimeout(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS).dispatcher(mDispatcher).build()).build();
}
use of okhttp3.logging.HttpLoggingInterceptor in project DevRing by LJYcoder.
the class RingModule method okHttpClient.
@Singleton
@Provides
OkHttpClient okHttpClient(Application application, OkHttpClient.Builder builder, HttpConfig httpConfig, HttpProgressInterceptor progressInterceptor) {
if (httpConfig.getConnectTimeout() > 0) {
builder.connectTimeout(httpConfig.getConnectTimeout(), TimeUnit.SECONDS);
}
if (httpConfig.isUseLog()) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(loggingInterceptor);
}
if (httpConfig.isUseCache()) {
HttpCacheInterceptor cacheInterceptor = new HttpCacheInterceptor(application, httpConfig.getCacheTimeWithNet(), httpConfig.getCacheTimeWithoutNet());
// 缓存目录
File cacheFile;
if (httpConfig.getCacheFolder() != null && httpConfig.getCacheFolder().isDirectory()) {
cacheFile = httpConfig.getCacheFolder();
} else {
cacheFile = FileUtil.getDirectory(FileUtil.getExternalCacheDir(application), "retrofit_http_cache");
}
// 大小默认20Mb
Cache cache = new Cache(cacheFile, httpConfig.getCacheSize() > 0 ? httpConfig.getCacheSize() : 1024 * 1024 * 20);
builder.addInterceptor(cacheInterceptor);
builder.addNetworkInterceptor(cacheInterceptor);
builder.cache(cache);
}
if (!CollectionUtil.isEmpty(httpConfig.getMapHeader())) {
HttpHeaderInterceptor headerInterceptor = new HttpHeaderInterceptor(httpConfig.getMapHeader());
builder.addInterceptor(headerInterceptor);
}
builder.addNetworkInterceptor(progressInterceptor);
return builder.build();
}
Aggregations