背景
在使用鴻洋大神的玩Android網(wǎng)站開放的api開發(fā)android app時,使用Retrofit請求登錄api,需要保存Cookie以備其他需要登陸后才能操作的api使用。
自定義攔截器實(shí)現(xiàn)持久化Cookie
首先定義響應(yīng)攔截器,該攔截器實(shí)現(xiàn)從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<String> cookies = response.headers("set-cookie");
String cookie = encodeCookie(cookies);
saveCookie(request.url().toString(), request.url().host(), cookie);
}
return response;
}
/**
* 整合cookie為唯一字符串
*/
private String encodeCookie(List<String> cookies) {
StringBuilder sb = new StringBuilder();
Set<String> 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設(shè)置相同的cookie,其中host可選
* 這樣能使得該cookie的應(yīng)用范圍更廣
*/
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;
}
}
最后將這兩個攔截器設(shè)置到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();
實(shí)際中的問題
在登錄后保存了Cookie,但是請求我的收藏時,第一次是可以得到的,第二次則提示需要登錄。輸出保存的Cookie內(nèi)容才發(fā)現(xiàn)其內(nèi)容改變了。于是,將請求登錄時只添加SaveCookiesInterceptor,其他請求只添加AddCookiesInterceptor。解決問題。
完整項(xiàng)目
鴻洋大神玩android網(wǎng)址→http://www.wanandroid.com/
完整項(xiàng)目地址→玩android