Retrofit2.0 ,OkHttp3完美同步持久Cookie实现免登录(二)

原文出自csdn:
http://blog.csdn.net/sk719887916/article/details/51700659;

通过对Retrofit2.0的<Retrofit 2.0 超能实践,完美支持Https传输>基础入门和案例实践,掌握了怎么样使用Retrofit访问网络,加入自定义header,包括加入SSL证书,基本的调试基础,但是正常的开发中会涉及cookie同步问题,可以实现一些自动或免登录登陆问题,接下来进入cookie同步姿势

Cookie

Cookies是一种能够让网站服务器把少量数据储存到客户端的硬盘或内存,或是从客户端的硬盘读取数据的一种技术。Cookies是当你浏览某网站时,由Web服务器置于你硬盘上的一个非常小的文本文件,它可以记录你的用户ID、密码、浏览过的网页、停留的时间等信息。当你再次来到该网站时,网站通过读取Cookies,得知你的相关信息,就可以做出相应的动作,如在页面显示欢迎你的标语,或者让你不用输入ID、密码就直接登录等等。

从本质上讲,它可以看作是你的身份证。但Cookies不能作为代码执行,也不会传送病毒,且为你所专有,并只能由提供它的服务器来读取。保存的信息片断以“名/值”对(name-value pairs)的形式储存,一个“名/值”对仅仅是一条命名的数据。一个网站只能取得它放在你的电脑中的信息,它无法从其它的Cookies文件中取得信息,也无法得到你的电脑上的其它任何东西。

Cookies中的内容大多数经过了加密处理,因此一般用户看来只是一些毫无意义的字母数字组合,只有服务器的CGI处理程序才知道它们真正的含义。

Cookie也是http的会话跟踪技术,也包含web端的session。cookie的作用就是为了解决HTTP协议无状态的缺陷所作的努力.

#Cookie结构

发送规则:

浏览器(设备)根据下面的几个规则决定是否发送某个Cookie信息:

  • 请求的主机名是否与某个存储的Cookie的Domain属性匹配;
  • 请求的端口号是否在该Cookie的Port属性列表中;
  • 请求的资源路径是否在该Cookie的Path属性指定的目录及子目录中;
  • 该Cookie的有效期是否已过。

下面是cookie代码示例:

     HTTP/1.1 200 OK
     Content-type: text/html
     Set-Cookie: name=value
     Set-Cookie: name2=value2; Expires=Wed, 09 Jun 2021 10:18:14 GMT
    (content of page)

Cookie请求头字段中的每个Cookie之间用逗号或分号分隔。

在Cookie请求头字段中除了必须有“名称=值”的设置外,还可以有Version、Path、Domain、Port等属性名之前,都要增加一个“$”字符作为前缀。

Version属性只能出现一次,且要位于Cookie请求头字段设置值的最前面,如果需要设置鞭个Cookie信息的Path、Domain、Port等属性,它们必须位于该Cookie信息的“名称=值”设置之后。

Path属性指向子目录的Cookie排在Path属性指向父目录的Cookie之前。

在Servlet程序中作用Cookie

Servlet API中提供了一个javax.servlet.http.Cookie类来封闭Cookie信息,它包含有生成Cookie信息和提取Cookie信息的各个属性的方法。

Cookie类的方法:

  • (构造方法:public Cookie(java.lang.String name,java.lang.String value)
  • getName方法
  • setValue与getValue方法
  • setMaxAge与getMaxAge方法
  • setPath与getPath方法
  • setDomian与getPath方法
  • setVersion与getVersion方法
  • setComment与getComment方法
  • setSecoure与getSecure方法

自定义cookie

HttpClient中大家都知道加入cookie的方式

AsyncHttpClient client = new AsyncHttpClient();
PersistentCookieStore myCookieStore = new PersistentCookieStore(MainActivity.this);
client.setCookieStore(myCookieStore);

因此Retrofit中需自我实现一个PersistentCookieStore 用来储存OkHttpCookies。

#方案一:

-PersistentCookieStore

/**
  * Created by LIUYONGKUI on 2016-06-09.
  */
public class PersistentCookieStore {
private static final String LOG_TAG = "PersistentCookieStore";
private static final String COOKIE_PREFS = "Cookies_Prefs";

private final Map<String, ConcurrentHashMap<String, Cookie>> cookies;
private final SharedPreferences cookiePrefs;


public PersistentCookieStore(Context context) {
    cookiePrefs = context.getSharedPreferences(COOKIE_PREFS, 0);
    cookies = new HashMap<>();

    //将持久化的cookies缓存到内存中 即map cookies
    Map<String, ?> prefsMap = cookiePrefs.getAll();
    for (Map.Entry<String, ?> entry : prefsMap.entrySet()) {
        String[] cookieNames = TextUtils.split((String) entry.getValue(), ",");
        for (String name : cookieNames) {
            String encodedCookie = cookiePrefs.getString(name
  • 10
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 30
    评论
在 Android 中使用 Retrofit 上传图片可以通过 `@Multipart` 和 `@Part` 注解实现。以下是 Retrofit 2.0 实现图文上传的方法总结: 1. 添加依赖库 在项目的 `build.gradle` 文件中添加以下依赖库: ```groovy implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.squareup.retrofit2:converter-scalars:2.9.0' ``` 2. 创建 Retrofit 实例 ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); ``` 3. 创建 API 接口 ```java public interface ApiService { @Multipart @POST("upload") Call<ResponseBody> upload( @Part("description") RequestBody description, @Part MultipartBody.Part file); } ``` 其中,`@Multipart` 注解表示这是一个包含文本和文件的表单;`@POST("upload")` 表示上传的 URL 地址;`@Part("description") RequestBody description` 表示上传的文本参数,`description` 是参数名,可以自己定义;`@Part MultipartBody.Part file` 表示上传的文件参数。 4. 创建请求参数 ```java File file = new File(filePath); RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile); RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), "description"); ``` 5. 发送网络请求 ```java ApiService apiService = retrofit.create(ApiService.class); Call<ResponseBody> call = apiService.upload(description, body); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { // 成功上传后的处理 } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { // 上传失败后的处理 } }); ``` 以上就是 Retrofit 2.0 实现图文上传的方法总结。注意,在 `AndroidManifest.xml` 文件中需要添加网络权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值