OKHttpログブロッカーの追加

1497 ワード

**

OKHttp


**
public class OKHttpUtil  {
    private static OKHttpUtil okHttpUtil;
    private final OkHttpClient client;

    // 
    private OKHttpUtil() {
        client = new OkHttpClient.Builder()
                .addInterceptor(new LogInterceptor())
                .build();
    }
    // 
    public static OKHttpUtil getInstance() {
        if (null == okHttpUtil) {
            synchronized (OKHttpUtil.class) {
                if (null == okHttpUtil) {
                    okHttpUtil = new OKHttpUtil();
                }
            }
        }
        return okHttpUtil;
    }

    //get
    public void get(String urlString, Callback callback) {
        Request request = new Request.Builder()
                .url(urlString).build();
        client.newCall(request).enqueue(callback);
    }
}

**

ログブロック


**
public class LogInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Log.e("LogInterceptor", "request:" + request);
        Log.e("LogInterceptor", "System.nanoTime():" + System.nanoTime());
        Response response = chain.proceed(request);
        Log.e("LogInterceptor", "request:" + request);
        Log.e("LogInterceptor", "System.nanoTime():" + System.nanoTime());
        return response;
    }
}