Search in sources :

Example 1 with HttpParams

use of com.lzy.okgo.model.HttpParams in project okhttp-OkGo by jeasonlzy.

the class GApp method initOkGo.

private void initOkGo() {
    // ---------这里给出的是示例代码,告诉你可以这么传,实际使用的时候,根据需要传,不需要就不传-------------//
    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", "这里支持中文参数");
    // ----------------------------------------------------------------------------------------//
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    // log相关
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
    // log打印级别,决定了log显示的详细程度
    loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
    // log颜色级别,决定了log在控制台显示的颜色
    loggingInterceptor.setColorLevel(Level.INFO);
    // 添加OkGo默认debug日志
    builder.addInterceptor(loggingInterceptor);
    // 第三方的开源库,使用通知显示当前请求的log,不过在做文件下载的时候,这个库好像有问题,对文件判断不准确
    // builder.addInterceptor(new ChuckInterceptor(this));
    // 超时时间设置,默认60秒
    // 全局的读取超时时间
    builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
    // 全局的写入超时时间
    builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
    // 全局的连接超时时间
    builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
    // 自动管理cookie(或者叫session的保持),以下几种任选其一就行
    // builder.cookieJar(new CookieJarImpl(new SPCookieStore(this)));            //使用sp保持cookie,如果cookie不过期,则一直有效
    // 使用数据库保持cookie,如果cookie不过期,则一直有效
    builder.cookieJar(new CookieJarImpl(new DBCookieStore(this)));
    // builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));            //使用内存保持cookie,app退出后,cookie消失
    // https相关设置,以下几种方案根据需要自己设置
    // 方法一:信任所有证书,不安全有风险
    HttpsUtils.SSLParams sslParams1 = HttpsUtils.getSslSocketFactory();
    // 方法二:自定义信任规则,校验服务端证书
    HttpsUtils.SSLParams sslParams2 = HttpsUtils.getSslSocketFactory(new SafeTrustManager());
    // 方法三:使用预埋证书,校验服务端证书(自签名证书)
    // HttpsUtils.SSLParams sslParams3 = HttpsUtils.getSslSocketFactory(getAssets().open("srca.cer"));
    // 方法四:使用bks证书和密码管理客户端证书(双向认证),使用预埋证书,校验服务端证书(自签名证书)
    // HttpsUtils.SSLParams sslParams4 = HttpsUtils.getSslSocketFactory(getAssets().open("xxx.bks"), "123456", getAssets().open("yyy.cer"));
    builder.sslSocketFactory(sslParams1.sSLSocketFactory, sslParams1.trustManager);
    // 配置https的域名匹配规则,详细看demo的初始化介绍,不需要就不要加入,使用不当会导致https握手失败
    builder.hostnameVerifier(new SafeHostnameVerifier());
    // 其他统一的配置
    // 详细说明看GitHub文档:https://github.com/jeasonlzy/
    // 必须调用初始化
    OkGo.getInstance().init(this).setOkHttpClient(// 建议设置OkHttpClient,不设置会使用默认的
    builder.build()).setCacheMode(// 全局统一缓存模式,默认不使用缓存,可以不传
    CacheMode.NO_CACHE).setCacheTime(// 全局统一缓存时间,默认永不过期,可以不传
    CacheEntity.CACHE_NEVER_EXPIRE).setRetryCount(// 全局统一超时重连次数,默认为三次,那么最差的情况会请求4次(一次原始请求,三次重连请求),不需要可以设置为0
    3).addCommonHeaders(// 全局公共头
    headers).addCommonParams(// 全局公共参数
    params);
}
Also used : HttpHeaders(com.lzy.okgo.model.HttpHeaders) HttpParams(com.lzy.okgo.model.HttpParams) OkHttpClient(okhttp3.OkHttpClient) CookieJarImpl(com.lzy.okgo.cookie.CookieJarImpl) DBCookieStore(com.lzy.okgo.cookie.store.DBCookieStore) HttpsUtils(com.lzy.okgo.https.HttpsUtils) HttpLoggingInterceptor(com.lzy.okgo.interceptor.HttpLoggingInterceptor)

Example 2 with HttpParams

use of com.lzy.okgo.model.HttpParams 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)

Example 3 with HttpParams

use of com.lzy.okgo.model.HttpParams in project BaseProject by feer921.

the class HttpUtils method generateMultipartRequestBody.

/**
 * 生成类似表单的请求体
 */
public static RequestBody generateMultipartRequestBody(HttpParams params, boolean isMultipart) {
    if (params.fileParamsMap.isEmpty() && !isMultipart) {
        // 表单提交,没有文件
        FormBody.Builder bodyBuilder = new FormBody.Builder();
        for (String key : params.urlParamsMap.keySet()) {
            List<String> urlValues = params.urlParamsMap.get(key);
            for (String value : urlValues) {
                bodyBuilder.add(key, value);
            }
        }
        return bodyBuilder.build();
    } else {
        // 表单提交,有文件
        MultipartBody.Builder multipartBodybuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        // 拼接键值对
        if (!params.urlParamsMap.isEmpty()) {
            for (Map.Entry<String, List<String>> entry : params.urlParamsMap.entrySet()) {
                List<String> urlValues = entry.getValue();
                for (String value : urlValues) {
                    multipartBodybuilder.addFormDataPart(entry.getKey(), value);
                }
            }
        }
        // 拼接文件
        for (Map.Entry<String, List<HttpParams.FileWrapper>> entry : params.fileParamsMap.entrySet()) {
            List<HttpParams.FileWrapper> fileValues = entry.getValue();
            for (HttpParams.FileWrapper fileWrapper : fileValues) {
                RequestBody fileBody = RequestBody.create(fileWrapper.contentType, fileWrapper.file);
                multipartBodybuilder.addFormDataPart(entry.getKey(), fileWrapper.fileName, fileBody);
            }
        }
        return multipartBodybuilder.build();
    }
}
Also used : FormBody(okhttp3.FormBody) HttpParams(com.lzy.okgo.model.HttpParams) MultipartBody(okhttp3.MultipartBody) List(java.util.List) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Example 4 with HttpParams

use of com.lzy.okgo.model.HttpParams in project okhttp-OkGo by jeasonlzy.

the class ServerApi method getData.

public static <T> Observable<T> getData(Type type, String url, String header, String param) {
    HttpHeaders headers = new HttpHeaders();
    headers.put("aaa", header);
    HttpParams params = new HttpParams();
    params.put("bbb", param);
    // 这个RxUtils的封装其实没有必要,只是有些人喜欢这么干,我就多此一举写出来了。。
    return RxUtils.request(HttpMethod.POST, url, type, params, headers);
}
Also used : HttpHeaders(com.lzy.okgo.model.HttpHeaders) HttpParams(com.lzy.okgo.model.HttpParams)

Example 5 with HttpParams

use of com.lzy.okgo.model.HttpParams in project okhttp-OkGo by jeasonlzy.

the class ServerApi method getString.

public static Observable<String> getString(String header, String param) {
    HttpHeaders headers = new HttpHeaders();
    headers.put("aaa", header);
    HttpParams params = new HttpParams();
    params.put("bbb", param);
    // 这个RxUtils的封装其实没有必要,只是有些人喜欢这么干,我就多此一举写出来了。。
    return RxUtils.request(HttpMethod.GET, Urls.URL_METHOD, String.class, params, headers);
}
Also used : HttpHeaders(com.lzy.okgo.model.HttpHeaders) HttpParams(com.lzy.okgo.model.HttpParams)

Aggregations

HttpParams (com.lzy.okgo.model.HttpParams)6 HttpHeaders (com.lzy.okgo.model.HttpHeaders)4 List (java.util.List)2 Map (java.util.Map)2 FormBody (okhttp3.FormBody)2 MultipartBody (okhttp3.MultipartBody)2 RequestBody (okhttp3.RequestBody)2 CookieJarImpl (com.lzy.okgo.cookie.CookieJarImpl)1 DBCookieStore (com.lzy.okgo.cookie.store.DBCookieStore)1 PersistentCookieStore (com.lzy.okgo.cookie.store.PersistentCookieStore)1 HttpsUtils (com.lzy.okgo.https.HttpsUtils)1 HttpLoggingInterceptor (com.lzy.okgo.interceptor.HttpLoggingInterceptor)1 FileNameMap (java.net.FileNameMap)1 CertificateException (java.security.cert.CertificateException)1 OkHttpClient (okhttp3.OkHttpClient)1