美文网首页
Okhttp拦截器

Okhttp拦截器

作者: 小梦想家北冥有鱼 | 来源:发表于2017-04-23 00:58 被阅读0次

    Interceptors

    Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls. Here's a simple interceptor that logs the outgoing request and the incoming response.

    application 拦截器,下面一个栗子会说明;

    插入器是一个很有用技巧 ,可以监控,重写和重新请求。下面举个栗子, 拦截器日志优于请求和即将到来响应;

    A call to chain.proceed(request)is a critical part of each interceptor’s implementation. This simple-looking method is where all the HTTP work happens, producing a response to satisfy the request.Interceptors can be chained. Suppose you have both a compressing interceptor and a check summing interceptor: you'll need to decide whether data is compressed and then checksummed, or checksummed and then compressed. OkHttp uses lists to track interceptors, and interceptors are called in order.

    一个请求 chain.proceed(request) 是一个临界的拦截器实现的部分,它简单可见的方法是对所有Http线程发生,生产响应对满意的请求,拦截器可以被束缚,支持你所有压缩拦截和求和拦截,你将需要决定是数据压缩然后检验,或者检验和再压缩。okhttp使用Lists集合去追踪拦截器,拦截器顺便被调用;

    网络请求 监听请求信息以及相应头信息

    Interceptors are registered as either application or network interceptors. We'll use theLoggingInterceptor defined above to show the difference.

    Register anapplication interceptor by calling addInterceptor() onOkHttpClient.Builder:

    拦截器注册既不是应用也非网络拦截器,我们将使用这个LoggingInterceptor定义显示不同之处,注册在Application拦截器 通过调用addInterceptor方法在okHttpClient.Builder;

    构建okHttpClient实例对象,添加拦截器

    执行结果: 

    执行结果当中,url重定向了,共花费3s多

    相关文章

      网友评论

          本文标题:Okhttp拦截器

          本文链接:https://www.haomeiwen.com/subject/cxtoattx.html