您现在的位置是:首页 > 文章详情

Retrofit--记使用Retrofit时遇到的一个坑

日期:2016-08-01点击:475
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/52098755

绪论

很久没有写博客了,上次是写了使用Retrofit时怎么设置OKHttp,如果你没看到请看:
Retrofit–使用Retrofit时怎样去设置OKHttp
你也可以看看我是怎样使用Retrofit的:
Retrofit–合理封装回调能让你的项目高逼格
好了,今天说一下我遇到的一个坑吧。

Retrofit Cookie问题

上次我们说过怎么去实现持久化登录,本地保存用户的cookie:
1.
这里写图片描述
2.
本地新建PersistentCookieStore文件保存SP中
3.添加依赖

compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.okio:okio:1.6.0' compile 'com.google.code.gson:gson:2.6.2' compile 'com.squareup.retrofit2:converter-gson:2.0.1' compile 'com.squareup.okhttp3:logging-interceptor:3.2.0' compile 'com.squareup.okhttp3:okhttp-urlconnection:3.0.0-RC1'

之前可能没说明添加哪些依赖,导致小伙伴们都问我出现了问题,这次说明一下。

我在做项目的时候遇到的问题就是:
测试环境下:我这样保存cookie是没有问题的,而且几个小伙伴也用了这个方法到项目中也OK,但是当我们的服务器由测试切到了正式环境下之后,cookie是可以获取到的,但是服务器却怎么都不识别,这着实让我很郁闷,我不得不猜想这是后台服务器的问题,但是后台大哥一直坚持他那里面没有问题,没办法,我最后又google了一下,这种方法可以用,先说一下:

同样给HttpClient添加拦截器:
这里写图片描述
看一下ReceivedCookiesInterceptor和AddCookiesInterceptor这两个文件:

package com.hankkin.bpm.http.cookie; import android.content.Context; import android.content.SharedPreferences; import java.io.IOException; import okhttp3.Interceptor; import okhttp3.Response; import rx.Observable; import rx.functions.Action1; import rx.functions.Func1; public class ReceivedCookiesInterceptor implements Interceptor { private Context context; public ReceivedCookiesInterceptor(Context context) { super(); this.context = context; } @Override public Response intercept(Chain chain) throws IOException { Response originalResponse = chain.proceed(chain.request()); if (!originalResponse.headers("Set-Cookie").isEmpty()) { final StringBuffer cookieBuffer = new StringBuffer(); Observable.from(originalResponse.headers("Set-Cookie")) .map(new Func1<String, String>() { @Override public String call(String s) { String[] cookieArray = s.split(";"); return cookieArray[0]; } }) .subscribe(new Action1<String>() { @Override public void call(String cookie) { cookieBuffer.append(cookie).append(";"); } }); SharedPreferences sharedPreferences = context.getSharedPreferences("cookie", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("cookie", cookieBuffer.toString()); editor.commit(); } return originalResponse; } }
package com.hankkin.bpm.http.cookie; import android.content.Context; import android.content.SharedPreferences; import java.io.IOException; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; import rx.Observable; import rx.functions.Action1; public class AddCookiesInterceptor implements Interceptor { private Context context; private String lang; public AddCookiesInterceptor(Context context,String lang) { super(); this.context = context; this.lang = lang; } @Override public Response intercept(Chain chain) throws IOException { final Request.Builder builder = chain.request().newBuilder(); SharedPreferences sharedPreferences = context.getSharedPreferences("cookie", Context.MODE_PRIVATE); Observable.just(sharedPreferences.getString("cookie", "")) .subscribe(new Action1<String>() { @Override public void call(String cookie) { if (cookie.contains("lang=ch")){ cookie = cookie.replace("lang=ch","lang="+lang); } if (cookie.contains("lang=en")){ cookie = cookie.replace("lang=en","lang="+lang); } //添加cookie builder.addHeader("Cookie", cookie); } }); return chain.proceed(builder.build()); } }
package com.hankkin.bpm.http.cookie; import android.content.Context; import android.content.SharedPreferences; import java.io.IOException; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; import rx.Observable; import rx.functions.Action1; public class AddCookiesInterceptor implements Interceptor { private Context context; private String lang; public AddCookiesInterceptor(Context context,String lang) { super(); this.context = context; this.lang = lang; } @Override public Response intercept(Chain chain) throws IOException { final Request.Builder builder = chain.request().newBuilder(); SharedPreferences sharedPreferences = context.getSharedPreferences("cookie", Context.MODE_PRIVATE); Observable.just(sharedPreferences.getString("cookie", "")) .subscribe(new Action1<String>() { @Override public void call(String cookie) { if (cookie.contains("lang=ch")){ cookie = cookie.replace("lang=ch","lang="+lang); } if (cookie.contains("lang=en")){ cookie = cookie.replace("lang=en","lang="+lang); } //添加cookie builder.addHeader("Cookie", cookie); } }); return chain.proceed(builder.build()); } }

大家可以忽略lang的东西,那些是多元化传给服务器的。
我最后的猜测也就是:服务器的正式和测试环境下他在设置cookie值的时候并没有在cookie里面设置完整,所以导致我从cookie里面取的值不正确或者不是最新的,当然这也只是我的猜测,很可能还和环境问题有关系。
总之我们又有了一种持久化管理cookie的方法,记录下来方便以后使用。
参考文章:
Retrofit使用OkHttp保存和添加cookie

原文链接:https://yq.aliyun.com/articles/641294
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章