Search in sources :

Example 1 with PersistentCookieStore

use of com.lzy.okgo.cookie.store.PersistentCookieStore in project okhttp-OkGo by jeasonlzy.

the class GApp method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    //---------这里给出的是示例代码,告诉你可以这么传,实际使用的时候,根据需要传,不需要就不传-------------//
    HttpHeaders headers = new HttpHeaders();
    //header不支持中文,不允许有特殊字符
    headers.put("commonHeaderKey1", "commonHeaderValue1");
    headers.put("commonHeaderKey2", "commonHeaderValue2");
    HttpParams params = new HttpParams();
    //param支持中文,直接传,不要自己编码
    params.put("commonParamsKey1", "commonParamsValue1");
    params.put("commonParamsKey2", "这里支持中文参数");
    //-----------------------------------------------------------------------------------//
    //必须调用初始化
    OkGo.init(this);
    //好处是全局参数统一,特定请求可以特别定制参数
    try {
        //以下都不是必须的,根据需要自行选择,一般来说只需要 debug,缓存相关,cookie相关的 就可以了
        OkGo.getInstance().debug("OkGo", Level.INFO, true).setConnectTimeout(//全局的连接超时时间
        OkGo.DEFAULT_MILLISECONDS).setReadTimeOut(//全局的读取超时时间
        OkGo.DEFAULT_MILLISECONDS).setWriteTimeOut(//全局的写入超时时间
        OkGo.DEFAULT_MILLISECONDS).setCacheMode(CacheMode.NO_CACHE).setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE).setRetryCount(3).setCookieStore(//cookie持久化存储,如果cookie不过期,则一直有效
        new PersistentCookieStore()).setCertificates().addCommonHeaders(//设置全局公共头
        headers).addCommonParams(//设置全局公共参数
        params);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PersistentCookieStore(com.lzy.okgo.cookie.store.PersistentCookieStore) HttpHeaders(com.lzy.okgo.model.HttpHeaders) HttpParams(com.lzy.okgo.model.HttpParams) CertificateException(java.security.cert.CertificateException)

Aggregations

PersistentCookieStore (com.lzy.okgo.cookie.store.PersistentCookieStore)1 HttpHeaders (com.lzy.okgo.model.HttpHeaders)1 HttpParams (com.lzy.okgo.model.HttpParams)1 CertificateException (java.security.cert.CertificateException)1