Android-分享一个常见的网络请求库(OkGo)
2022-08-23 本文已影响0人
阿博聊编程
图片来源网络,入侵必删
在常见的Android
项目维护当中,我们可能会遇到一个网络请求库OkGo
。这篇文章分享一下OkGo
的相关知识,希望能帮小伙伴们快速上。
OkGo
一个常见的网络请求库,基于okhttp的标准RESTful风格的网络框架。
OkGo的特性
- 工程结构全新优化
- 支持RxJava
- 支持RxJava2
- 支持自定义缓存策略
- 支持下载管理
- 支持上传管理
导入项目
//必须使用
compile 'com.lzy.net:okgo:3.0.4'
//以下三个选择添加,okrx和okrx2不能同时使用
compile 'com.lzy.net:okrx:1.0.2'
compile 'com.lzy.net:okrx2:2.0.2'
compile 'com.lzy.net:okserver:2.0.5'
截止我发布博客的时候,最新版本是3.0.4
。最新版本请看开源库的wiki
混淆代码
#okhttp
-dontwarn okhttp3.**
-keep class okhttp3.**{*;}
#okio
-dontwarn okio.**
-keep class okio.**{*;}
#okgo
-dontwarn com.lzy.okgo.**
-keep class com.lzy.okgo.**{*;}
#okrx
-dontwarn com.lzy.okrx.**
-keep class com.lzy.okrx.**{*;}
#okrx2
-dontwarn com.lzy.okrx2.**
-keep class com.lzy.okrx2.**{*;}
#okserver
-dontwarn com.lzy.okserver.**
-keep class com.lzy.okserver.**{*;}
OkGo的使用
初始化,一般在Aplication,或者基类中配置,只需要调用一次即可,可以设置下面的属性:
- 可以配置log开关
- 全局的超时时间
- 全局cookie管理策略
- Https配置
- 超时重连次数
- 公共的请求头和请求参数等信息
最简单的初始化
OkGo.getInstance().init(this);
构建OkHttpClient.Builder:
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);
builder.addInterceptor(loggingInterceptor);
//非必要情况,不建议使用,第三方的开源库,使用通知显示当前请求的log,不过在做文件下载的时候,这个库好像有问题,对文件判断不准确
builder.addInterceptor(new ChuckInterceptor(this));
配置超时时间:
//全局的读取超时时间
builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
//全局的写入超时时间
builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
//全局的连接超时时间
builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
请求的封装
直接分享一个开源库大佬对请求的封装:
/*
* Copyright 2016 jeasonlzy(廖子尧)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lzy.demo.okrx2;
import com.lzy.demo.callback.JsonConvert;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpMethod;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.request.base.Request;
import com.lzy.okrx2.adapter.ObservableBody;
import java.lang.reflect.Type;
import io.reactivex.Observable;
/**
* ================================================
* 作 者:jeasonlzy(廖子尧)Github地址:https://github.com/jeasonlzy
* 版 本:1.0
* 创建日期:2017/5/28
* 描 述:
* 修订历史:
* ================================================
*/
public class RxUtils {
public static <T> Observable<T> request(HttpMethod method, String url, Type type) {
return request(method, url, type, null);
}
public static <T> Observable<T> request(HttpMethod method, String url, Type type, HttpParams params) {
return request(method, url, type, params, null);
}
public static <T> Observable<T> request(HttpMethod method, String url, Type type, HttpParams params, HttpHeaders headers) {
return request(method, url, type, null, params, headers);
}
public static <T> Observable<T> request(HttpMethod method, String url, Class<T> clazz) {
return request(method, url, clazz, null);
}
public static <T> Observable<T> request(HttpMethod method, String url, Class<T> clazz, HttpParams params) {
return request(method, url, clazz, params, null);
}
public static <T> Observable<T> request(HttpMethod method, String url, Class<T> clazz, HttpParams params, HttpHeaders headers) {
return request(method, url, null, clazz, params, headers);
}
/**
* 这个封装其实没有必要,只是有些人喜欢这么干,我就多此一举写出来了。。
* 这个封装其实没有必要,只是有些人喜欢这么干,我就多此一举写出来了。。
* 这个封装其实没有必要,只是有些人喜欢这么干,我就多此一举写出来了。。
*/
public static <T> Observable<T> request(HttpMethod method, String url, Type type, Class<T> clazz, HttpParams params, HttpHeaders headers) {
Request<T, ? extends Request> request;
if (method == HttpMethod.GET) request = OkGo.get(url);
else if (method == HttpMethod.POST) request = OkGo.post(url);
else if (method == HttpMethod.PUT) request = OkGo.put(url);
else if (method == HttpMethod.DELETE) request = OkGo.delete(url);
else if (method == HttpMethod.HEAD) request = OkGo.head(url);
else if (method == HttpMethod.PATCH) request = OkGo.patch(url);
else if (method == HttpMethod.OPTIONS) request = OkGo.options(url);
else if (method == HttpMethod.TRACE) request = OkGo.trace(url);
else request = OkGo.get(url);
request.headers(headers);
request.params(params);
if (type != null) {
request.converter(new JsonConvert<T>(type));
} else if (clazz != null) {
request.converter(new JsonConvert<T>(clazz));
} else {
request.converter(new JsonConvert<T>());
}
return request.adapt(new ObservableBody<T>());
}
}