android自动登录简书,Android 自动登录——持久化Cookie

背景

在使用鸿洋大神的玩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。解决问题。

完整项目

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值