Search in sources :

Example 46 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project xabber-android by redsolution.

the class HttpApiManager method getPushRetrofit.

private static Retrofit getPushRetrofit() {
    if (retrofitPush == null) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        // if debug enable http logging
        if (BuildConfig.DEBUG)
            httpClientBuilder.addInterceptor(loggingInterceptor);
        OkHttpClient httpClient = httpClientBuilder.build();
        Gson gson = new GsonBuilder().setLenient().create();
        retrofitPush = new Retrofit.Builder().baseUrl(XABBER_PUSH_API_URL).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create(gson)).client(httpClient).build();
    }
    return retrofitPush;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 47 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project iNGAGE by davis123123.

the class UserRecentCommentHandler method enqueue.

// save recent comment in database
public void enqueue(String username, String thread_id, String messageText, String side, String ip) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(logging);
    String url = "http://" + ip + "/track_user_comment.php/";
    Retrofit retrofit = new Retrofit.Builder().client(httpClient.build()).addConverterFactory(GsonConverterFactory.create()).baseUrl(url).build();
    Interface service = retrofit.create(Interface.class);
    Call<ResponseBody> call = service.post(username, thread_id, messageText, side, ip);
    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();
                    Log.i("STATE", "Retrofit reponse: " + serverResponse);
                } 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");
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 48 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project android-oss by kickstarter.

the class ApplicationModule method provideHttpLoggingInterceptor.

@Provides
@Singleton
@NonNull
static HttpLoggingInterceptor provideHttpLoggingInterceptor() {
    final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    return interceptor;
}
Also used : HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) NonNull(androidx.annotation.NonNull) Provides(dagger.Provides)

Example 49 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project okhttp-OkGo by jeasonlzy.

the class GApp method initOkGo.

private void initOkGo() {
    // ---------这里给出的是示例代码,告诉你可以这么传,实际使用的时候,根据需要传,不需要就不传-------------//
    HttpHeaders headers = new HttpHeaders();
    // header不支持中文,不允许有特殊字符
    headers.put("commonHeaderKey1", "commonHeaderValue1");
    headers.put("commonHeaderKey2", "commonHeaderValue2");
    HttpParams params = new HttpParams();
    // param支持中文,直接传,不要自己编码
    params.put("commonParamsKey1", "commonParamsValue1");
    params.put("commonParamsKey2", "这里支持中文参数");
    // ----------------------------------------------------------------------------------------//
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    // log相关
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
    // log打印级别,决定了log显示的详细程度
    loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
    // log颜色级别,决定了log在控制台显示的颜色
    loggingInterceptor.setColorLevel(Level.INFO);
    // 添加OkGo默认debug日志
    builder.addInterceptor(loggingInterceptor);
    // 第三方的开源库,使用通知显示当前请求的log,不过在做文件下载的时候,这个库好像有问题,对文件判断不准确
    // builder.addInterceptor(new ChuckInterceptor(this));
    // 超时时间设置,默认60秒
    // 全局的读取超时时间
    builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
    // 全局的写入超时时间
    builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
    // 全局的连接超时时间
    builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
    // 自动管理cookie(或者叫session的保持),以下几种任选其一就行
    // builder.cookieJar(new CookieJarImpl(new SPCookieStore(this)));            //使用sp保持cookie,如果cookie不过期,则一直有效
    // 使用数据库保持cookie,如果cookie不过期,则一直有效
    builder.cookieJar(new CookieJarImpl(new DBCookieStore(this)));
    // builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));            //使用内存保持cookie,app退出后,cookie消失
    // https相关设置,以下几种方案根据需要自己设置
    // 方法一:信任所有证书,不安全有风险
    HttpsUtils.SSLParams sslParams1 = HttpsUtils.getSslSocketFactory();
    // 方法二:自定义信任规则,校验服务端证书
    HttpsUtils.SSLParams sslParams2 = HttpsUtils.getSslSocketFactory(new SafeTrustManager());
    // 方法三:使用预埋证书,校验服务端证书(自签名证书)
    // HttpsUtils.SSLParams sslParams3 = HttpsUtils.getSslSocketFactory(getAssets().open("srca.cer"));
    // 方法四:使用bks证书和密码管理客户端证书(双向认证),使用预埋证书,校验服务端证书(自签名证书)
    // HttpsUtils.SSLParams sslParams4 = HttpsUtils.getSslSocketFactory(getAssets().open("xxx.bks"), "123456", getAssets().open("yyy.cer"));
    builder.sslSocketFactory(sslParams1.sSLSocketFactory, sslParams1.trustManager);
    // 配置https的域名匹配规则,详细看demo的初始化介绍,不需要就不要加入,使用不当会导致https握手失败
    builder.hostnameVerifier(new SafeHostnameVerifier());
    // 其他统一的配置
    // 详细说明看GitHub文档:https://github.com/jeasonlzy/
    // 必须调用初始化
    OkGo.getInstance().init(this).setOkHttpClient(// 建议设置OkHttpClient,不设置会使用默认的
    builder.build()).setCacheMode(// 全局统一缓存模式,默认不使用缓存,可以不传
    CacheMode.NO_CACHE).setCacheTime(// 全局统一缓存时间,默认永不过期,可以不传
    CacheEntity.CACHE_NEVER_EXPIRE).setRetryCount(// 全局统一超时重连次数,默认为三次,那么最差的情况会请求4次(一次原始请求,三次重连请求),不需要可以设置为0
    3).addCommonHeaders(// 全局公共头
    headers).addCommonParams(// 全局公共参数
    params);
}
Also used : HttpHeaders(com.lzy.okgo.model.HttpHeaders) HttpParams(com.lzy.okgo.model.HttpParams) OkHttpClient(okhttp3.OkHttpClient) CookieJarImpl(com.lzy.okgo.cookie.CookieJarImpl) DBCookieStore(com.lzy.okgo.cookie.store.DBCookieStore) HttpsUtils(com.lzy.okgo.https.HttpsUtils) HttpLoggingInterceptor(com.lzy.okgo.interceptor.HttpLoggingInterceptor)

Example 50 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project AndLang by wugemu.

the class HttpU method newOkHttpClient.

public OkHttpClient newOkHttpClient() {
    HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
    logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    if (openDns) {
        return // 设置读取超时时间
        new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
        TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
        TimeUnit.SECONDS).addNetworkInterceptor(// 添加网络请求日志
        logInterceptor).dns(new HttpDns()).build();
    } else if (openLog) {
        return // 设置读取超时时间
        new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
        TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
        TimeUnit.SECONDS).addNetworkInterceptor(// 添加网络请求日志
        logInterceptor).build();
    } else if (BaseLangUtil.isHaveSDPer()) {
        // 开启本地存储权限后使用缓存机制
        // 新建一个cache,指定目录为外部目录下的okhttp_cache目录,大小为100M
        Cache cache = new Cache(PicSelUtil.getCacheDir(), 100 * 1024 * 1024);
        return // 设置读取超时时间
        new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
        TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
        TimeUnit.SECONDS).cache(// 缓存设置
        cache).addInterceptor(// 请求网络拦截
        new RequestCacheI()).build();
    } else {
        // 新建一个cache,指定目录为外部目录下的okhttp_cache目录,大小为100M
        return // 设置读取超时时间
        new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
        TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
        TimeUnit.SECONDS).build();
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Dispatcher(okhttp3.Dispatcher) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache)

Aggregations

HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)107 OkHttpClient (okhttp3.OkHttpClient)87 Provides (dagger.Provides)27 Retrofit (retrofit2.Retrofit)25 GsonBuilder (com.google.gson.GsonBuilder)24 Singleton (javax.inject.Singleton)24 Cache (okhttp3.Cache)19 Request (okhttp3.Request)16 File (java.io.File)15 Response (okhttp3.Response)15 IOException (java.io.IOException)14 Gson (com.google.gson.Gson)13 Interceptor (okhttp3.Interceptor)11 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)6 LoggingInterceptor (com.toshi.manager.network.interceptor.LoggingInterceptor)6 List (java.util.List)5 ErrorInterceptor (me.postaddict.instagram.scraper.interceptor.ErrorInterceptor)5 FakeBrowserInterceptor (me.postaddict.instagram.scraper.interceptor.FakeBrowserInterceptor)5 ResponseBody (okhttp3.ResponseBody)5 BeforeClass (org.junit.BeforeClass)5