美文网首页
Retrofit 常用功能介绍

Retrofit 常用功能介绍

作者: younger_lei | 来源:发表于2018-01-19 20:00 被阅读0次

用Retrofit已经一年多了吧,越来越觉的好用、简单、漂亮。一次不可能把所有的功能都用上,所以我准备时常更新这篇文章,方便日后查看、复习。

统一设置头信息Cookie

如果在每个请求中设置的话太反锁了,所以应该在过滤器中统一设置,当cookie不为空时,说明是第一次请求,获取保存cookie

.addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Response response = chain.proceed(chain.request());
                        if (!response.headers("Set-Cookie").isEmpty()){
                            StringBuffer stringBuffer = new StringBuffer();
                            String session = response.headers("Set-Cookie").get(0);
                            stringBuffer.append(session.substring(0, session.indexOf(";")));
                            stringBuffer.append(";");
                            save(stringBuffer.toString());//保存cookie代码
                        }
                        return response;
                    }
                })

然后再添加一个过滤器,每当请求数据时将保存的cookie加入

.addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request.Builder builder = chain.request().newBuilder();
                        if (GlobalApplication.getLoginSession() != null)
                            builder.addHeader("Cookie",getCookie());//获取之前保存的cookie
                        return chain.proceed(builder.build());
                    }
                })

相关文章

  • Retrofit 常用功能介绍

    用Retrofit已经一年多了吧,越来越觉的好用、简单、漂亮。一次不可能把所有的功能都用上,所以我准备时常更新这篇...

  • Retrofit 源码解析

    1. 功能介绍 1.1 Retrofit Retrofit 是 Github 上面 squre 组织开发的一个类型...

  • Retrofit介绍

    本文是对Retrofit官网的翻译 1.介绍(Intorduction) 简单说来Retrofit实现的功能就是将...

  • day6 阿来

    安装并载入包,dplyr包常用功能介绍 安装和载入包 dplyr包常用功能介绍 总结 要多熟悉常用的R包功能

  • python之random模块简介

    前言: 这里只介绍一些常用的功能

  • Retrofit库

    Retrofit介绍 Retrofit是Square公司基于Restful风格推出的网络框架封装Retrofit是...

  • Android Retrofit 工作原理解析

    本文以 Retrofit 整合 RxJava 为例,介绍 Retrofit 的工作原理,使用 Retrofit 2...

  • Retrofit

    Retrofit介绍 Retrofit是Square公司基于RESTful风格推出的网络框架封装 Retrofit...

  • Hystrix常用功能介绍

    Hystrix是一个简单易用的熔断中间件,本篇文章会介绍下常规的使用方式。 目录 helloWorld初窥Hyst...

  • Flutter 常用功能介绍

    Json序列化和反序列化 首先,默认的JSON.decode是将一个json格式的string 转化成一个Map ...

网友评论

      本文标题:Retrofit 常用功能介绍

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