Search in sources :

Example 16 with HttpLoggingInterceptor

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();
            }
        }
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) File(java.io.File) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache)

Example 17 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project 2017-01-HUDI-MAC-CHAR by NHNNEXT.

the class ServiceGenerator method createService.

public static <S> S createService(Class<S> serviceClass, Context context) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    if (BuildConfig.DEBUG) {
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    } else {
        logging.setLevel(HttpLoggingInterceptor.Level.NONE);
    }
    PersistentCookieStore cookieStore = new PersistentCookieStore(context);
    java.net.CookieManager cookieManager = new java.net.CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder().cookieJar(new JavaNetCookieJar(cookieManager)).addInterceptor(logging).addNetworkInterceptor(new StethoInterceptor());
    Gson gson = new GsonBuilder().setLenient().create();
    Retrofit retorfit = new Retrofit.Builder().baseUrl(MafiaRemoteService.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).client(httpClient.build()).build();
    return retorfit.create(serviceClass);
}
Also used : PersistentCookieStore(com.zimincom.mafiaonline.PersistentCookieStore) JavaNetCookieJar(okhttp3.internal.JavaNetCookieJar) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Retrofit(retrofit2.Retrofit) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 18 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project Atom_Android by Rogrand-Dev.

the class HttpModule method provideClient.

@Singleton
@Provides
OkHttpClient provideClient(OkHttpClient.Builder builder) {
    if (DemoApp.getAppContext().isDebug()) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {

            @Override
            public void log(String message) {
                LogUtils.i(TAG, message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // 设置 Log 拦截器
        builder.addInterceptor(loggingInterceptor);
    }
    File cacheFile = new File(DataUtils.getCacheFile(DemoApp.getAppContext()), "RograndCache");
    // 100 MB
    int cacheSize = 100 * 1024 * 1024;
    Cache cache = new Cache(cacheFile, cacheSize);
    Interceptor cacheInterceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            CacheControl.Builder cacheBuilder = new CacheControl.Builder();
            cacheBuilder.maxAge(0, TimeUnit.SECONDS);
            cacheBuilder.maxStale(365, TimeUnit.DAYS);
            CacheControl cacheControl = cacheBuilder.build();
            Request request = chain.request();
            if (!NetUtils.isNetworkAvailable(DemoApp.getAppContext())) {
                request = request.newBuilder().cacheControl(cacheControl).build();
            }
            Response response = chain.proceed(request);
            if (NetUtils.isNetworkAvailable(DemoApp.getAppContext())) {
                // 有网络时, 不缓存, 最大保存时长为0
                int maxAge = 0;
                response.newBuilder().header("Cache-Control", "public, max-age=" + maxAge).removeHeader("Pragma").build();
            } else {
                // 无网络时,设置超时为4周
                int maxStale = 60 * 60 * 24 * 28;
                response.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale).removeHeader("Pragma").build();
            }
            return response;
        }
    };
    // 设置 header(设置 Token、API 版本号)
    Interceptor headerInterceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            request = request.newBuilder().addHeader("Authorization", "Bearer " + SPUtils.get(DemoApp.getAppContext(), Constants.ACCESS_TOKEN, "")).addHeader("api_version", UrlConstants.API_VERSION).build();
            return chain.proceed(request);
        }
    };
    // 设置统一的请求头部参数
    builder.addInterceptor(headerInterceptor);
    // 设置缓存
    builder.addNetworkInterceptor(cacheInterceptor);
    builder.addInterceptor(cacheInterceptor);
    builder.cache(cache);
    // 设置连接超时
    builder.connectTimeout(TIME_OUT, TimeUnit.SECONDS);
    // 设置读取数据超时
    builder.readTimeout(TIME_OUT, TimeUnit.SECONDS);
    // 设置写入数据超时
    builder.writeTimeout(TIME_OUT, TimeUnit.SECONDS);
    // 设置 Stetho
    builder.addNetworkInterceptor(new StethoInterceptor());
    // 设置 Cookie
    builder.cookieJar(new CookieManger(DemoApp.getAppContext()));
    // 错误重连
    builder.retryOnConnectionFailure(true);
    return builder.build();
}
Also used : CookieManger(com.rogrand.demo.http.cookie.CookieManger) Request(okhttp3.Request) Response(okhttp3.Response) CacheControl(okhttp3.CacheControl) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) File(java.io.File) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) Interceptor(okhttp3.Interceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 19 with HttpLoggingInterceptor

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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 20 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project AnDevCon-RxPatterns by colintheshots.

the class Example7 method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_example7);
    mTextView = (TextView) findViewById(R.id.example7_textView);
    mTextView2 = (TextView) findViewById(R.id.example7_textView2);
    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).build();
    mWeatherInterface = new Retrofit.Builder().baseUrl("http://api.openweathermap.org").client(client).addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())).addConverterFactory(GsonConverterFactory.create(gson)).build().create(WeatherInterface.class);
    // Since we're returning a basic type from Observable.zip()
    // instead of an observable of a type, we can simply map to
    // the new type. In fact, the cast() operator is probably
    // good enough here.
    Observable.zip(mIntegerObservable1, mIntegerObservable2, new Func2<Integer, Integer, Integer>() {

        @Override
        public Integer call(Integer integer, Integer integer2) {
            return integer + integer2;
        }
    }).map(new Func1<Integer, String>() {

        @Override
        public String call(Integer integer) {
            return Integer.toString(integer);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

        @Override
        public void call(String s) {
            mTextView.setText(s);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
        }
    });
    Observable.zip(mObservable1, mObservable2, new Func2<String, String, Observable<CityWeather>>() {

        @Override
        public Observable<CityWeather> call(String lat, String lon) {
            return mWeatherInterface.getWeather(lat, lon);
        }
    }).flatMap(new Func1<Observable<CityWeather>, Observable<CityWeather>>() {

        @Override
        public Observable<CityWeather> call(Observable<CityWeather> cityWeatherObservable) {
            return cityWeatherObservable;
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<CityWeather>() {

        @Override
        public void call(CityWeather cityWeather) {
            mTextView2.setText("City: " + cityWeather.name + "\nTemp: " + Float.toString(cityWeather.main.temp) + " Kelvins");
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Observable(rx.Observable) Retrofit(retrofit2.Retrofit) Func1(rx.functions.Func1) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Func2(rx.functions.Func2)

Aggregations

HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)22 OkHttpClient (okhttp3.OkHttpClient)17 Provides (dagger.Provides)11 Retrofit (retrofit2.Retrofit)10 Singleton (javax.inject.Singleton)9 GsonBuilder (com.google.gson.GsonBuilder)7 File (java.io.File)6 Cache (okhttp3.Cache)6 GsonConverterFactory (retrofit2.converter.gson.GsonConverterFactory)6 Observable (rx.Observable)6 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)5 List (java.util.List)5 Interceptor (okhttp3.Interceptor)5 Activity (android.app.Activity)4 Bundle (android.os.Bundle)4 ListView (android.widget.ListView)4 Gson (com.google.gson.Gson)4 Expose (com.google.gson.annotations.Expose)4 RxJavaCallAdapterFactory (retrofit2.adapter.rxjava.RxJavaCallAdapterFactory)4 Context (android.content.Context)3