Android 拦截器

LoggingInterceptor(拦截器页面)

class LoggingInterceptor implements Interceptor {
  @Override
  public Response intercept(Interceptor.Chain chain) throws IOException {
    //拿到Request对象
    Request request = chain.request();

    long t1 = System.nanoTime();
    System.out.println(" request  = " + String.format("Sending request %s on %s%n%s",
            request.url(), chain.connection(), request.headers()));

    //拿到Response对象
    Response response = chain.proceed(request);

    long t2 = System.nanoTime();
    //得出请求网络,到得到结果,中间消耗了多长时间
    System.out.println("response  " + String.format("Received response for %s in %.1fms%n%s",
            response.request().url(), (t2 - t1) / 1e6d, response.headers()));
    return response;
  }
}

OkhtttpUtilsTwo(工具包)

/**
 * date:2018/11/19
 * author:(dell)
 * function:
 * 1.因为okhttp代码太多,太冗余,使用的时候不是太方便,两行代码搞定                                         -----doGet,doPost
 * 2.都要创建OKhttp和咱们的handler对象,对象创建太多,导致内存过多的消耗                                   ----单例
 * 3.异步请求okhttp,数据请求成功以后,数据在子线程,所以我们还要写handler,把数据放到主线程去,逻辑复杂         -----handler和接口
 *
 * 1. okhttp,handler,单例模式,接口
 *
 */
public class OkhtttpUtilsTwo {
    单例//
    private final Handler mHandler;
    private final OkHttpClient mOkHttpClient;
    private static OkhtttpUtilsTwo sOkhtttpUtilsTwo;

    //构造方法不私有
    private OkhtttpUtilsTwo(){
        //如果创建Handler的对象,是在一个普通的类里创建,那么一定要加上Looper.getMainLooper()这个参数
        mHandler = new Handler(Looper.getMainLooper());


        LoggingInterceptor loggingInterceptor = new LoggingInterceptor();

        mOkHttpClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .readTimeout(5000, TimeUnit.MILLISECONDS)
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000, TimeUnit.MILLISECONDS)
                .build();
    }

    //单例暴露一个普通的方法,给对方,双重锁模式
    public static OkhtttpUtilsTwo getInstance(){
        if (sOkhtttpUtilsTwo  ==null){
            synchronized (OkhtttpUtilsTwo.class){
                if (sOkhtttpUtilsTwo == null){
                    return sOkhtttpUtilsTwo =new OkhtttpUtilsTwo();
                }
            }
        }
        return sOkhtttpUtilsTwo;
    }
    //接口
    public interface OkCallback {
        void onFailure (Exception e);
        void onResponse(String json);
    }
    okhttp与handler///
    /封装doGEt的网络封装,参数定义两个,一个是URL网址   一个实现接口的对象
    public void doGet(String url , final OkCallback ycfOkCallback){
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                if (ycfOkCallback !=null){
                    //切换到主线程
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            ycfOkCallback.onFailure(e);
                        }
                    });
                }
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response !=null && response.isSuccessful()){
                    final String json = response.body().string();
                    if (ycfOkCallback !=null){
                        //切换到主线程
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                ycfOkCallback.onResponse(json);
                            }
                        });
                    }
                }

            }
        });
    }

    //封装doPost的逻辑代码
    public void doPost(String url, Map<String,String> map, final OkCallback ycfOkCallback){
        //创建FormBody对象,把表单添加到FormBody
        FormBody.Builder builder = new FormBody.Builder();
        //集合对象不为null的情况下
        if (map != null){
            for(String key: map.keySet()){
                builder.add(key,map.get(key));
            }
        }

        FormBody formBody = builder.build();

        //创建Request对象
        Request request = new Request.Builder()
                .post(formBody)
                .url(url)
                .build();

        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                if (ycfOkCallback !=null){
                    //切换到主线程
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            ycfOkCallback.onFailure(e);
                        }
                    });
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response !=null && response.isSuccessful()){
                    final String json = response.body().string();
                    if (ycfOkCallback !=null){
                        //切换到主线程
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                ycfOkCallback.onResponse(json);
                            }
                        });
                    }
                }
            }
        });

    }



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值