背景
在使用鸿洋大神的玩Android网站开放的api开发android app时,使用Retrofit请求登录api,需要保存Cookie以备其他需要登陆后才能操作的api使用。
自定义拦截器实现持久化Cookie
首先定义响应拦截器,该拦截器实现从response获取set-cookie字段的值,并将其保存在本地。
public class SaveCookiesInterceptor implements Interceptor {
private static final String COOKIE_PREF = "cookies_prefs";
private Context mContext;
protected SaveCookiesInterceptor(Context context) {
mContext = context;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
//set-cookie可能为多个
if (!response.headers("set-cookie").isEmpty()) {
List cookies = response.headers("set-cookie");
String cookie = encodeCookie(cookies);
saveCookie(request.url().toString(), request.url().host(), cookie);
}
return response;
}
/**
* 整合cookie为唯一字符串
*/
private String encodeCookie(List cookies) {
StringBuilder sb = new StringBuilder();
Set set = new HashSet<>();
for (String cookie : cookies) {
String[] arr = cookie.split(";");
for (String s : arr) {
if (set.contains(s)) {
continue;
}
set.add(s);
}
}
for (String cookie : set) {
sb.append(cookie).append(";");
}
int last = sb.lastIndexOf(";");
if (sb.length() - 1 == last) {
sb.deleteCharAt(last);
}
return sb.toString();
}
/**
* 保存cookie到本地,这里我们分别为该url和host设置相同的cookie,其中host可选
* 这样能使得该cookie的应用范围更广
*/
private void saveCookie(String url, String domain, String cookies) {
SharedPreferences sp = mContext.getSharedPreferences(COOKIE_PREF, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if (TextUtils.isEmpty(url)) {
throw new NullPointerException("url is null.");
} else {
editor.putString(url, cookies);
}
if (!TextUtils.isEmpty(domain)) {
editor.putString(domain, cookies);
}
editor.apply();
}
/**
* 清除本地Cookie
*
* @param context Context
*/
public static void clearCookie(Context context) {
SharedPreferences sp = context.getSharedPreferences(COOKIE_PREF, Context.MODE_PRIVATE);
sp.edit().clear().apply();
}
}
然后定义请求拦截器,如果该请求存在cookie,则为其添加到Header的Cookie中。
public class AddCookiesInterceptor implements Interceptor {
private static final String COOKIE_PREF = "cookies_prefs";
private Context mContext;
public AddCookiesInterceptor(Context context) {
mContext = context;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder builder = request.newBuilder();
String cookie = getCookie(request.url().toString(), request.url().host());
if (!TextUtils.isEmpty(cookie)) {
builder.addHeader("Cookie", cookie);
}
return chain.proceed(builder.build());
}
private String getCookie(String url, String domain) {
SharedPreferences sp = mContext.getSharedPreferences(COOKIE_PREF, Context.MODE_PRIVATE);
if (!TextUtils.isEmpty(url) && sp.contains(url) && !TextUtils.isEmpty(sp.getString(url, ""))) {
return sp.getString(url, "");
}
if (!TextUtils.isEmpty(domain) && sp.contains(domain) && !TextUtils.isEmpty(sp.getString(domain, ""))) {
return sp.getString(domain, "");
}
return null;
}
}
最后将这两个拦截器设置到OkHttpClient
OkHttpClient client = builder.addInterceptor(new AddCookiesInterceptor(context))
.addInterceptor(new SaveCookiesInterceptor(context))
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.API_BASE)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
实际中的问题
在登录后保存了Cookie,但是请求我的收藏时,第一次是可以得到的,第二次则提示需要登录。输出保存的Cookie内容才发现其内容改变了。于是,将请求登录时只添加SaveCookiesInterceptor,其他请求只添加AddCookiesInterceptor。解决问题。
完整项目
鸿洋大神玩android网址→http://www.wanandroid.com/
完整项目地址→玩android