# BaseOkHttp V3
<a href="https://github.com/kongzue/BaseOkHttp/">
<img src="https://img.shields.io/badge/BaseOkHttp-3.2.1-green.svg" alt="BaseOkHttp">
</a>
<a href="https://bintray.com/myzchh/maven/BaseOkHttp_v3/3.1.9/link">
<img src="https://img.shields.io/badge/Maven-3.2.1-blue.svg" alt="Maven">
</a>
<a href="http://www.apache.org/licenses/LICENSE-2.0">
<img src="https://img.shields.io/badge/License-Apache%202.0-red.svg" alt="License">
</a>
<a href="http://www.kongzue.com">
<img src="https://img.shields.io/badge/Homepage-Kongzue.com-brightgreen.svg" alt="Homepage">
</a>
## 简介
- 无需操心线程问题,请求在异步线程执行,回调会自动回到主线程;
- 提供统一返回监听器 ResponseListener 处理一切返回数据,无论是错误还是成功,都可以一起处理,对于相同操作代码不再需要重复,避免代码反复臃肿。
- 提供基于 Map 对象和 List 对象的 Json 解析库及数据类型,直接与适配器配合,抛弃编写 JavaBean 的麻烦;
- 强大的全局方法和事件让您的请求得心应手。
- BaseOkHttp V3是基于BaseOkHttp V2( <https://github.com/kongzue/BaseOkHttp> )的升级版本,基于能够快速创建常用请求链接而封装的库。
## Maven仓库或Gradle的引用方式
### jitPack 方式
请先在 'build.gradle(project)' 中添加如下代码:
```
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
在 'build.gradle(app)' 中引入:
```
dependencies {
//BaseOkHttp V3 网络请求库
implementation 'com.github.kongzue:BaseOkHttpV3:3.2.4.2'
//BaseJson 解析库
implementation 'com.github.kongzue:BaseJson:1.0.7.2'
}
```
额外的,如果您的项目未引入 okHttp,还需要添加 okHttp 最新版本依赖:
```
dependencies {
implementation "com.squareup.okhttp3:okhttp:4.9.1"
}
```
### jCenter 方式
因 jCenter 停止维护,不再推荐使用。
Maven仓库:
```
<dependency>
<groupId>com.kongzue.baseokhttp_v3</groupId>
<artifactId>baseokhttp_v3</artifactId>
<version>3.2.2</version>
<type>pom</type>
</dependency>
```
Gradle:
在dependencies{}中添加引用:
```
//BaseOkHttp V3 网络请求库
implementation 'com.kongzue.baseokhttp_v3:baseokhttp_v3:3.2.2'
//BaseJson 解析库
implementation 'com.kongzue.basejson:basejson:1.0.7'
```
新版本系统(API>=27)中,使用非 HTTPS 请求地址可能出现 java.net.UnknownServiceException 错误,解决方案请参考:<https://www.jianshu.com/p/528a3def1cf4>
![BaseOkHttpV3 Demo](https://github.com/kongzue/Res/raw/master/app/src/main/res/mipmap-xxxhdpi/baseokhttpv3demo.png)
试用版可以前往 <http://beta.kongzue.com/BaseOkHttp3> 下载
## 目录
· <a href="#一般请求">一般请求</a>
· <a href="#json请求">JSON请求</a>
· <a href="#文件上传">文件上传</a>
· <a href="#文件下载">文件下载</a>
· <a href="#putdelete">PUT&DELETE</a>
· <a href="#websocket">WebSocket</a>
· <a href="#json解析">JSON解析</a>
· <a href="#javabean解析">JavaBean解析</a>
· <a href="#额外功能">额外功能</a>
···· <a href="#全局日志">全局日志</a>
···· <a href="#全局请求地址">全局请求地址</a>
···· <a href="#全局-Header-请求头">全局 Header 请求头</a>
···· <a href="#全局请求返回拦截器">全局请求返回拦截器</a>
···· <a href="#HTTPS-支持">HTTPS 支持</a>
···· <a href="#全局参数拦截器">全局参数拦截器</a>
···· <a href="#请求超时">请求超时</a>
···· <a href="#停止请求">停止请求</a>
···· <a href="#容灾地址">容灾地址</a>
···· <a href="#Cookie">Cookie</a>
· <a href="#开源协议">开源协议</a>
· <a href="#更新日志">更新日志</a>
## 一般请求
BaseOkHttp V3 提供两种请求写法,范例如下:
#### 以参数形式创建请求:
```
progressDialog = ProgressDialog.show(context, "请稍候", "请求中...");
HttpRequest.POST(context, "http://你的接口地址", new Parameter().add("page", "1"), new ResponseListener() {
@Override
public void onResponse(String response, Exception error) {
progressDialog.dismiss();
if (error == null) {
resultHttp.setText(response);
} else {
resultHttp.setText("请求失败");
Toast.makeText(context, "请求失败", Toast.LENGTH_SHORT).show();
}
}
});
```
Parameter 对象是一个 Map 的封装对象,可以通过 `.toParameterString()` 方法获得按 key 首字母排序、以“&”符号连接的文本串,或者使用 `.toParameterJson()` 方法将其转为 JSONObject。
一般请求中,使用 HttpRequest.POST(...) 方法可直接创建 POST 请求,相应的,`HttpRequest.GET(...)` 可创建 GET 请求,另外可选额外的方法增加 header 请求头:
```
HttpRequest.POST(Context context, String url, Parameter headers, Parameter parameter, ResponseListener listener);
HttpRequest.GET(Context context, String url, Parameter headers, Parameter parameter, ResponseListener listener);
```
#### 以流式代码创建请求:
```
progressDialog = ProgressDialog.show(context, "请稍候", "请求中...");
HttpRequest.build(context,"http://你的接口地址")
.addHeaders("Charset", "UTF-8")
.addParameter("page", "1")
.addParameter("token", "A128")
.setResponseListener(new ResponseListener() {
@Override
public void onResponse(String response, Exception error) {
progressDialog.dismiss();
if (error == null) {
resultHttp.setText(response);
} else {
resultHttp.setText("请求失败");
Toast.makeText(context, "请求失败", Toast.LENGTH_SHORT).show();
}
}
})
.doPost();
```
返回回调监听器只有一个,请在其中对 error 参数判空,若 error 不为空,则为请求失败,反之则为请求成功,请求成功后的数据存放在 response 参数中。
之所以将请求成功与失败放在一个回调中主要目的是方便无论请求成功或失败都需要执行的代码,例如上述代码中的 progressDialog 等待对话框都需要关闭(dismiss掉),这样的写法更为方便。
3.1.0 版本起提供直接解析返回值为 jsonMap 对象,可使用 `JsonResponseListener` 监听器返回:
```
HttpRequest.POST(context, "/femaleNameApi", new Parameter().add("page", "1"), new JsonResponseListener() {
@Override
public void onResponse(JsonMap main, Exception error) {
if (error == null) {
resultHttp.setText(main.getString("msg"));
} else {
resultHttp.setText("请求失败");
Toast.makeText(context, "请求失败", Toast.LENGTH_SHORT).show();
}
}
});
```
#### 关于返回线程的说明
BaseOkHttpV3 会智能的判断需要返回线程的场景,一般而言,您无需为此而操心,您可以直接在请求回调中进行 UI 的操作。
BaseOkHttpV3 的请求过程是异步进行的(多线程),而在返回数据后,是根据 context 进行判断的是要在异步线程执行还是在 UI 线程执行,当 context 传入的是 Activity 时,默认会在 UI 线程返回,无需额外处理线程问题,而当 context 为非 Activity,例如 ApplicationContext 或者 Service 时,则会在异步线程返回数据,若需要修改界面上的元素显示,您需要手动切换到主线程刷新 UI 组件。
这是在我们经过大量的使用场景调研后得出的最优设计方案,但如若您的请求返回数据流非常大,可能造成 UI 线程卡顿,建议在传入 context 时,使用 `context.getApplicationContext()` 方法来强制异步线程返回,待数据得到妥善处理后,需要返回 UI 线程刷新界面显示时,使用 `activity.runOnUiThread(...)
没有合适的资源?快使用搜索试试~ 我知道了~
OkHttp的二次封装.zip
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共73个文件
java:34个
xml:13个
png:10个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 5 0 下载量 15 浏览量
2024-07-06
14:53:21
上传
评论
收藏 242KB ZIP 举报
温馨提示
http请求方法 OkHttp的二次封装库,提供各种快速使用方法以及更为方便的扩展功能。提供更高效的Json请求和解析工具以及文件上传下载封装,HTTPS和Cookie操作也更得心应手。
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 73 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/31980fae9bcd442e8a55faf7a68724b2_qq_24428851.jpg!1)
武昌库里写JAVA
- 粉丝: 5455
- 资源: 3041
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 8051Proteus仿真c源码舞蹈机器人步进机仿真,C51程序通过
- BLDC 无刷电机 脉冲注入 启动法 启动过程持续插入正反向短时脉冲;定位准,启动速度快; Mcu:华大hc32f030; 功能
- 8051Proteus仿真c源码无线遥控应用程序与仿真
- STM32+74HC595驱动4位数码管显示0-3.zip
- 8051Proteus仿真c源码无线遥控实现
- 8051Proteus仿真c源码无软件消抖的独立式键盘输入实验
- 8051Proteus仿真c源码我做的单片机水塔控制系统
- 8051Proteus仿真c源码文件包应用举例
- 8051Proteus仿真c源码温度控制的实例
- MATLAB 神经网络43个案例分析
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)