com.lzy.okgo.cookie.store.SPCookieStore Java Examples
The following examples show how to use
com.lzy.okgo.cookie.store.SPCookieStore.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OkGoUtils.java From DevUtils with Apache License 2.0 | 4 votes |
/** * 初始化 OkGo 配置 * <pre> * @see <a href="https://github.com/jeasonlzy/okhttp-OkGo/wiki/Init"/> * tips: 最简单的配置直接调用 OkGo.getInstance().init(this); {@link OkGo} * 在 App {@link Application} 初始化 * </pre> * @param application {@link Application} */ public static void initOkGo(Application application) { OkHttpClient.Builder builder = new OkHttpClient.Builder(); // 自定义日志拦截 JSON 打印 builder.addInterceptor(new dev.other.okgo.HttpLoggingInterceptor()); // ======================== // = OkGo 内置 log 拦截器 = // ======================== HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo"); // log 打印级别, 决定了 log 显示的详细程度 loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY); // log 颜色级别, 决定了 log 在控制台显示的颜色 loggingInterceptor.setColorLevel(Level.INFO); builder.addInterceptor(loggingInterceptor); // =============== // = 配置超时时间 = // =============== // 全局的读取超时时间 builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS); // 全局的写入超时时间 builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS); // 全局的连接超时时间 builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS); // ============================= // = Cookie 持久化 ( Session ) = // ============================= // 使用 sp 保持 cookie, 如果 cookie 不过期, 则一直有效 builder.cookieJar(new CookieJarImpl(new SPCookieStore(application))); // // 使用数据库保持 cookie, 如果 cookie 不过期, 则一直有效 // builder.cookieJar(new CookieJarImpl(new DBCookieStore(application))); // // 使用内存保持 cookie, app 退出后, cookie 消失 // builder.cookieJar(new CookieJarImpl(new MemoryCookieStore())); // ============== // = Https 配置 = // ============== // // 方法一: 信任所有证书, 不安全有风险 // HttpsUtils.SSLParams sslParams1 = HttpsUtils.getSslSocketFactory(); // // 方法二: 自定义信任规则, 校验服务端证书 // HttpsUtils.SSLParams sslParams2 = HttpsUtils.getSslSocketFactory(new SafeTrustManager()); // // 方法三: 使用预埋证书, 校验服务端证书 ( 自签名证书 ) // HttpsUtils.SSLParams sslParams3 = HttpsUtils.getSslSocketFactory(application.getAssets().open("srca.cer")); // // 方法四: 使用 bks 证书和密码管理客户端证书 ( 双向认证 ) , 使用预埋证书, 校验服务端证书 ( 自签名证书 ) // HttpsUtils.SSLParams sslParams4 = HttpsUtils.getSslSocketFactory(application.getAssets().open("xxx.bks"), // "123456", application.getAssets().open("yyy.cer")); // builder.sslSocketFactory(sslParams1.sSLSocketFactory, sslParams1.trustManager); // // 配置 https 的域名匹配规则, 详细看 demo 的初始化介绍, 不需要就不要加入, 使用不当会导致 https 握手失败 // builder.hostnameVerifier(new SafeHostnameVerifier()); HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(); builder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager); builder.hostnameVerifier(HttpsUtils.UnSafeHostnameVerifier); // =============== // = 全局公共数据 = // =============== // header 不支持中文, 不允许有特殊字符 HttpHeaders headers = new HttpHeaders(); headers.put("commonHeaderKey1", "commonHeaderValue1"); headers.put("commonHeaderKey2", "commonHeaderValue2"); // param 支持中文, 直接传, 不要自己编码 HttpParams params = new HttpParams(); params.put("commonParamsKey1", "commonParamsValue1"); params.put("commonParamsKey2", "这里支持中文参数"); // ======================= // = 初始化 ( 应用 ) 配置 = // ======================= OkGo.getInstance().init(application) // 建议设置 OkHttpClient, 不设置将使用默认的 .setOkHttpClient(builder.build()) // 全局统一缓存模式, 默认不使用缓存, 可以不传 .setCacheMode(CacheMode.NO_CACHE) // 全局统一缓存时间, 默认永不过期, 可以不传 .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE) // 全局统一超时重连次数, 默认为三次, 那么最差的情况会请求 4 次 ( 一次原始请求, 三次重连请求 ), 不需要可以设置为 0 .setRetryCount(3) // 全局公共头 .addCommonHeaders(headers) // 全局公共参数 .addCommonParams(params); }
Example #2
Source File: MyApplication.java From PocketEOS-Android with GNU Lesser General Public License v3.0 | 4 votes |
public SPCookieStore getSPCookieStore() { return mSPCookieStore; }
Example #3
Source File: MyApplication.java From PocketEOS-Android with GNU Lesser General Public License v3.0 | 4 votes |
public void setSPCookieStore(SPCookieStore SPCookieStore) { mSPCookieStore = SPCookieStore; }
Example #4
Source File: MyApplication.java From PocketEOS-Android with GNU Lesser General Public License v3.0 | 4 votes |
@Override public void onCreate() { super.onCreate(); mInstance = this; Utils.init(getApplicationContext()); AutoLayoutConifg.getInstance().useDeviceSize(); //配置数据库 setupDatabase(); setTencent(Tencent.createInstance("1106779320", getApplicationContext()));//QQ第三方登录1106779320 LocalManageUtil.setApplicationLanguage(getApplicationContext()); setSPCookieStore(new SPCookieStore(this)); try { initOkGo(); } catch (IOException e) { e.printStackTrace(); } }
Example #5
Source File: MyApplication.java From PocketEOS-Android with GNU Lesser General Public License v3.0 | 4 votes |
public void initOkGo() throws IOException { HttpHeaders headers = new HttpHeaders(); if (new SPCookieStore(this).getAllCookie().size() != 0) { headers.put("Set-Cookie", String.valueOf(mSPCookieStore.getCookie(HttpUrl.parse(BaseUrl.HTTP_Get_code_auth)))); } headers.put("version", "3.0"); if (Utils.getSpUtils().getString("loginmode", "").equals("phone")) { headers.put("uid", MyApplication.getDaoInstant().getUserBeanDao().queryBuilder().where(UserBeanDao.Properties.Wallet_phone.eq(Utils.getSpUtils().getString("firstUser"))).build().unique().getWallet_uid()); } else { headers.put("uid", "6f1a8e0eb24afb7ddc829f96f9f74e9d"); } OkHttpClient.Builder builder = new OkHttpClient.Builder(); //log相关 HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkHttp"); loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY); //log打印级别,决定了log显示的详细程度 loggingInterceptor.setColorLevel(Level.INFO); //log颜色级别,决定了log在控制台显示的颜色 builder.addInterceptor(loggingInterceptor); //添加OkGo默认debug日志 //超时时间设置 builder.readTimeout(10000, TimeUnit.MILLISECONDS); //全局的读取超时时间 builder.writeTimeout(10000, TimeUnit.MILLISECONDS); //全局的写入超时时间 builder.connectTimeout(10000, TimeUnit.MILLISECONDS); //全局的连接超时时间 builder.cookieJar(new CookieJarImpl(mSPCookieStore)); //使用sp保持cookie,如果cookie不过期,则一直有效 HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(getAssets().open("server.cer")); builder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager); // //配置https的域名匹配规则,使用不当会导致https握手失败 builder.hostnameVerifier(HttpsUtils.UnSafeHostnameVerifier); // 其他统一的配置 OkGo.getInstance().init(this) //必须调用初始化 .setOkHttpClient(builder.build()) //必须设置OkHttpClient .setCacheMode(CacheMode.NO_CACHE) //全局统一缓存模式,默认不使用缓存,可以不传 .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE) //全局统一缓存时间,默认永不过期,可以不传 .setRetryCount(3) //全局统一超时重连次数,默认为三次,那么最差的情况会请求4次(一次原始请求,三次重连请求),不需要可以设置为0 .addCommonHeaders(headers); //全局公共头 // .addCommonParams(httpParams); //全局公共参数 }