android retrofit cookie怎么处理

android retrofit cookie怎么处理,第1张

首先是抽象的基类

public abstract class BaseApi {

public static final String API_SERVER = "服务器地址"

private static final OkHttpClient mOkHttpClient = new OkHttpClient()

private static Retrofit mRetrofit

protected static Retrofit getRetrofit() {

if (Retrofit == null) {

Context context = Application.getInstance().getApplicationContext()

//设定30秒超时

mOkHttpClient.setConnectTimeout(30, TimeUnit.SECONDS)

//设置拦截器,以用于自定义Cookies的设置

mOkHttpClient.networkInterceptors()

.add(new CookiesInterceptor(context))

//设置缓存目录

File cacheDirectory = new File(context.getCacheDir()

.getAbsolutePath(), "HttpCache")

Cache cache = new Cache(cacheDirectory, 20 * 1024 * 1024)

mOkHttpClient.setCache(cache)

//构建Retrofit

mRetrofit = new Retrofit.Builder()

//配置服务器路径

.baseUrl(API_SERVER + "/")

//设置日期解析格式,这样可以直接解析Date类型

.setDateFormat("yyyy-MM-dd HH:mm:ss")

//配置转化库,默认是Gson

.addConverterFactory(ResponseConverterFactory.create())

//配置回调库,采用RxJava

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

//设置OKHttpClient为网络客户端

.client(mOkHttpClient)

.build()

}

return mRetrofit

}

}1234567891011121314151617181920212223242526272829303132333435

然后是Cookies拦截器

public class CookiesInterceptor implements Interceptor{

private Context context

public CookiesInterceptor(Context context) {

this.context = context

}

//重写拦截方法,处理自定义的Cookies信息

@Override

public Response intercept(Chain chain) throws IOException {

Request request = chain.request()

Request compressedRequest = request.newBuilder()

.header("cookie", CookieUtil.getCookies(context))

.build()

Response response = chain.proceed(compressedRequest)

CookieUtil.saveCookies(response.headers(), context)

return response

}

}123456789101112131415161718

CookieUtil则是一些自定义解析和生成方法以及SharedPreferences的存取,代码略

然后是Api类

public class UserApi extends BaseApi{

//定义接口

private interface UserService {

//GET注解不可用@FormUrlEncoded,要用@Query注解引入请求参数

@GET("user/user_queryProfile")

Observable<UserProfileResp>queryProfile(@Query("userId") int userId)

//POST方法没有缓存,适用于更新数据

@FormUrlEncoded

@POST("user/user_updateUserName")

Observable<BaseResp>updateUserName(@Field("userName") String userName)

}

protected static final UserService service = getRetrofit().create(UserService.class)

//查询用户信息接口

public static Observable<UserProfileResp>queryProfile(int userId){

return service.queryProfile(userId)

}

//更新用户名接口

public static Observable<BaseResp>updateUserName(String userName){

return service.updateUserName(userName)

}

}123456789101112131415161718192021222324

再就是将Retrofit的响应消息经过Gson解析成期望的数据结构,称之为Model类

上文的BaseResp和UserProfileResp则是自定义的Model

假定服务器约定返回的Json格式为

{

"result":"结果代号,0表示成功",

"msg":"异常信息,仅在失败时返回数据",

"userInfo":

{

"id":"用户id",

"userName":"用户名名字"

}

}123456789

那么UserProfileResp可以写成

public class UserProfileResp {

//@SerializedName是指定Json格式中的Key名

//可以不写,则默认采用与变量名一样的Key名

@SerializedName("userInfo")

private UserProfileModel userInfo

public UserProfileModel getUserInfo() {

return userInfo

}

}12345678910

UserProfileModel则是具体的数据结构

public class UserProfileModel {

private int userId

private String userName

public String getUserName(){

return userName

}

}12345678

需要注意的是,如果没有使用@SerializedName指定Key名,当工程被混淆时,变量名会被混淆得与期望的Key名不符。因此需要将这类Model类统一放到一个工程目录,再在proguard-project文件中加入排除项

//不混淆Model类

-keep class com.xxx.model.xxx.** { *}12

最后是实际调用

public void getProfile(int userId){

UserApi.queryProfile(userId)

.subscribeOn(Schedulers.io())

.subscribe(new Subscriber<UserProfileResp>(){

@Override

public void onCompleted() {

}

@Override

public void onError(Throwable e) {

}

@Override

public void onNext(UserProfileResp userProfileResp) {

}

})

}

Biscuit、cookies和cracker都是饼干的意思,但是所指的饼干种类有所不同。而且即使是同一个单词,比如cracker,在不的国家(如英国和美国)也会有所不同。要想正确区分他们的意思,首先应该从他们的英文解释入手。当然,如果你正好有机会到英美国家去,进入到他们的超市,Biscuit、cookies和cracker的区别就不需要任何解释了,一目了然。

Biscuit:

a small flat dry cake for one person, usually sweet, and baked until crisp.(牛津高阶第7版)

Cookie

a small flat sweet cake for one person, usually baked unitl crisp.

Cracker

a thin dry biscuit that is often salt and usually eatern with cheese.

以上是来自牛津高阶第7版的解释。

Biscuit和Cookie:甜饼干

从上面我们可以看出,biscuit和cookie并没有什么区别,都是指饼干、点心,尤其是甜味的饼干。有些还会有夹心的。最深见的,比如奥利奥,就是一种cookie,此外还各种口味的饼干,如巧克力饼干、牛奶饼干等等。

biscuit甜饼干

Cracker:咸饼干

则是指比较薄,通常为咸味的一种小饼干,有的小苏打味比较重。这种饼干一般会配以其他调味品食用。比如奶酪、草莓酱等。英美人有喜欢喝下午茶的习惯,cracker是他们下午茶不可或缺的一种小食品。cracker咸饼干

idhttp1.POST('http://passport.donews.com/login',dl)

dhttp1.get('http://hi.donews.com/vote.jsp?id='+ EdIt2.TEXT)

两个url不一样,cookie 内容也不一样。

也就是说 cookie 是跟url邦定的


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11411092.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存