<commit_before>package com.xhy.weibo.api;

import android.text.TextUtils;

import com.xhy.weibo.utils.HttpNetUtil;
import com.xhy.weibo.utils.Logger;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xuhaoyang on 16/7/19.
 */
public class ApiClient {
    private static final String TAG = ApiClient.class.getSimpleName();
    private static ApiClientImpl api;

    private static OkHttpClient mOkHttpClient;

    private static final Interceptor cacheInterceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            if (!HttpNetUtil.isConnected()) {
                request = request.newBuilder()
                        //强制使用缓存
                        .cacheControl(CacheControl.FORCE_CACHE).build();
            }
            Response response = chain.proceed(request);

            if (HttpNetUtil.isConnected()) {
                String cacheControl = request.cacheControl().toString();
                return response.newBuilder()
                        .header("Cache-Control", cacheControl)
                        .addHeader("Accept", "application/json")
                        .removeHeader("Pragma")//移除干扰信息
                        .build();
            } else {
                int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
                return response.newBuilder()
                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .removeHeader("Pragma")
                        .build();
            }

        }
    };

    static {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
                .addInterceptor(cacheInterceptor)
                .addInterceptor(httpLoggingInterceptor)
                .retryOnConnectionFailure(true)//错误重连
                .connectTimeout(10, TimeUnit.SECONDS)//设置超时时间
                .readTimeout(10, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(10, TimeUnit.SECONDS);//设置写入超时时间
//        int cacheSize = 10 * 1024 * 1024; // 10 MiB
//        Cache cache = new Cache(.getContext().getCacheDir(), cacheSize);
//        builder.cache(cache);
//        builder.addInterceptor(interceptor);
        mOkHttpClient = builder.build();<commit_msg>fix long time rest 504bug<commit_after>package com.xhy.weibo.api;

import android.text.TextUtils;

import com.xhy.weibo.utils.HttpNetUtil;
import com.xhy.weibo.utils.Logger;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xuhaoyang on 16/7/19.
 */
public class ApiClient {
    private static final String TAG = ApiClient.class.getSimpleName();
    private static ApiClientImpl api;

    private static OkHttpClient mOkHttpClient;

    private static final Interceptor cacheInterceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            if (!HttpNetUtil.isConnected()) {
                request = request.newBuilder()
                        //强制使用缓存
                        .cacheControl(CacheControl.FORCE_CACHE).build();
            }
            Response response = chain.proceed(request);

            if (HttpNetUtil.isConnected()) {
                String cacheControl = request.cacheControl().toString();
                return response.newBuilder()
                        .header("Cache-Control", cacheControl)
                        .addHeader("Accept", "application/json")
                        .removeHeader("Pragma")//移除干扰信息
                        .build();
            } else {
                int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
                return response.newBuilder()
                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .removeHeader("Pragma")
                        .build();
            }

        }
    };

    static {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
//                现在尚未在服务端配合,会出现 504 Unsatisfiable Request (only-if-cached)错误
//                .addInterceptor(cacheInterceptor)
                .addInterceptor(httpLoggingInterceptor)
                .retryOnConnectionFailure(true)//错误重连
                .connectTimeout(10, TimeUnit.SECONDS)//设置超时时间
                .readTimeout(10, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(10, TimeUnit.SECONDS);//设置写入超时时间
//        int cacheSize = 10 * 1024 * 1024; // 10 MiB
//        Cache cache = new Cache(.getContext().getCacheDir(), cacheSize);
//        builder.cache(cache);
//        builder.addInterceptor(interceptor);
        mOkHttpClient = builder.build();
{'fraction_non_alphanumeric': 0.10042507970244421, 'fraction_numerical': 0.015232022670917464, 'mean_word_length': 2.1407119021134595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5005865', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1553, 'n_words': 294}