之前的更新方式

一搜cookies的使用,非常easy搜到非常多文章。主要的几步大致同样。例如以下图:

基本上都要求大家先调用cookieManager.removeAllCookie()或者调用 cookieManager.removeSessionCookie()。这样确实每一次的setCookie都能写入新的cookie,简单粗暴有效。

遇到的问题

大家看setCookies的方法:

 /**
* Sets a cookie for the given URL. Any existing cookie with the same host,
* path and name will be replaced with the new cookie. The cookie being set
* will be ignored if it is expired.
*
* @param url the URL for which the cookie is to be set
* @param value the cookie as a string, using the format of the 'Set-Cookie'
* HTTP response header
*/ public void setCookie(String url, String value) {
throw new MustOverrideException();
}

每个cookies是相应一个url的

再看removeSessionCookie


/**
* Removes all session cookies, which are cookies without an expiration
* date.
* @deprecated use {@link #removeSessionCookies(ValueCallback)} instead.
*/
public void removeSessionCookie() {
throw new MustOverrideException();
}

这可不是仅仅清掉了你所使用的那个url,而是清掉了全部的cookie,这就不正确了,你怎么能任意清除掉全部的cookie呢。说不定其它的同事针对另外一个url写入了相相应的cookie,说不定h5的同学也在cookie中写入了非常多实用的东西,你调用cookieManager.removeSessionCookie()后就是把全部的cookie清空了。

cookieManager.removeAllCookie()也是一样的道理

解决的方法

请细致看setCookie在Api中的方法注解:

* Sets a cookie for the given URL. Any existing cookie with the same host,
* path and name will be replaced with the new cookie. The cookie being set
* will be ignored if it is expired.

非常明显,你假设仅仅想更新你自己使用的url的cookie的话,不用删除cookies,仅仅要再次setCookie,系统会自己主动检查之前这个url有没有cookie。假设有,就替换掉。假设没有,就直接写入。

注意

CookieSyncManager跟CookieManager.removeAllCookie()、CookieManager.removeSessionCookie()都已经废弃,在api21以上的sok中同步cookie提供了 cookieManager.flush(),所以正确的setCookie应该例如以下:


String url = null;
url = "http://higo.xxx.xxx";
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
String cookies = "cookie";
cookieManager.setCookie(url, "xxx=" +cookies);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
} else {
CookieSyncManager.createInstance(this.getApplicationContext());
CookieSyncManager.getInstance().sync();
}

cookie中分号的使用

cookie中默认就是以分号切割的

比方你写入这种cookie

String cookie = "name=cookie;year=2015";
setCookie("http://xxx.xxx",cookie);

那事实上仅仅把name=cookie写入到了http://xxx.xxx这个域名下,为什么year=2015没有写入呢,由于分号“。”是cookie默认的切割符,cookie觉得出现“;”当前的cookie的值就结束了。

那能不能不使用“;”去;连接字符串呢?

最好不要这样做,由于大家都觉得cookie的分隔符就是“;”,假设你换成了第二种。当有的同事不知道的话,就出问题了。

正确的方式应该是怎么样的呢?

使用base64转码一下就能够了,这样做你还能把信息加密一次,当然须要你跟h5的同学沟通一下,他那边拿到cookie的值须要base64一下。这样就完美了。

希望能帮到你。

Android cookies正确的更新方式的更多相关文章

  1. Android中的内存管理机制以及正确的使用方式

    概述 从操作系统的角度来说,内存就是一块数据存储区域,属于可被操作系统调度的资源.现代多任务(进程)的操作系统中,内存管理尤为重要,操作系统需要为每一个进程合理的分配内存资源,所以可以从两方面来理解操 ...

  2. Android 数据存储五种方式

    1.概述 Android提供了5种方式来让用户保存持久化应用程序数据.根据自己的需求来做选择,比如数据是否是应用程序私有的,是否能被其他程序访问,需要多少数据存储空间等,分别是: ① 使用Shared ...

  3. Android如何正确的保存文件

    在Android 官方开发文档中有一篇文档来介绍如何保存应用的数据,但笔者用过很多程序(从知名的到不知名的)处理的都不是很完美,或者 没有按照Android 开发团队建议的方式去保存他们应用的数据.当 ...

  4. android 定位的四种方式

    [原文]  开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方式,希望对大家有帮助: android 定 ...

  5. android中正确导入第三方jar包

    android中正确导入第三方jar包 andriod中如果引入jar包的方式不对就会出现一些奇怪的错误. 工作的时候恰好有一个jar包需要调用,结果用了很长时间才解决出现的bug. 刚开始是这样引用 ...

  6. Android 中LocalBroadcastManager的使用方式

    Android 中LocalBroadcastManager的使用方式 在android-support-v4.jar中引入了LocalBroadcastManager,称为局部通知管理器,这种通知的 ...

  7. Android Service 通知Activity更新界面的方法研究

    Android Service 通知Activity更新界面的方法研究   Android的最重要的组件式service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题. ...

  8. Android MVP模式简单易懂的介绍方式 (一)

    Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 最近正在研究Android的MVP模式 ...

  9. ArcGis Android 10.2.6更新文档翻译

    ArcGis Android 10.2.6更新文档翻译 @[ArcGis Android|10.2.6|更新文档] 本文描述了ArcGIS Runtime SDK for Android 10.2.6 ...

随机推荐

  1. Biorhythms(poj 1006)

    Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰 ...

  2. 【SPOJ220】Relevant Phrases of Annihilation(后缀数组,二分)

    题意: n<=10,len<=1e4 思路: #include<cstdio> #include<cstring> #include<string> # ...

  3. DP的序--Codeforces626F. Group Projects

    $n \leq 200$个数,$ \leq 500$,$K \leq 1000$代价内的数字分组有多少?一个分组的代价是分成的每个小组的总代价:一个小组的代价是极差. 问的极差那就从极入手嘛.一个小组 ...

  4. SQLite数据库相关操作

    一.创建数据库 这里创建了note便签数据表,字段有noteId.noteTitle.noteTime.noteInfo );  // TODO Auto-generated constructor ...

  5. 更改App名称

    To change the installed application name, in Xcode: 1. Select your Target on the left side under Gro ...

  6. Mongodb报错:ERROR: child process failed, exited with error number 1

    Mongodb在启动时报错: 2018-10-16T11:18:54.533+0800 I CONTROL [main] Automatically disabling TLS 1.0, to for ...

  7. linux的sar命令未找到

    linux的sar命令未找到 一般的命令可以直接使用yum安装,但是sar和mpstat命令这两个命令都是在sysstat包里, 网上的解决方法:rpm -ivh gd-2.0.32-23.2.i58 ...

  8. CODEVS_2144 砝码称重 2 折半搜索+二分查找+哈希

    #include<iostream> #include<algorithm> #include<cstring> #include<map> #incl ...

  9. Jenkins中的Job配置里缺少“触发远程构建(例如,使用脚本)”选项的问题解决

    如图所示的功能没有出现在Job配置页面,这是由于权限问题导致的,解决方法如下: 1.[系统管理]->[Configure Global Security] 2.配置如下: 3.或者你有第三方权限 ...

  10. 【IntelliJ Idea】启动参数JVM参数的配置 优先级高于 application.yaml/application.properties中的配置,前者可以覆盖后者的配置