OkHttp3 使用
导入
compile 'com.squareup.okhttp3:okhttp:3.3.0'
GET请求
String url = "https://www.baidu.com/";
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
POST请求
private void DownloadFile() {
OkHttpClient.Builder client = new OkHttpClient.Builder();
//设置超时
client.connectTimeout(20, TimeUnit.SECONDS).writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).build();
OkhttpUtil.allowAllSSL(client);
//传递的参数通过add连接
RequestBody formBody = new FormBody.Builder().add("key1", value1)
.add("key2", key2).add("key3", key3).build();
okhttp3.Request request = new okhttp3.Request.Builder().url(downloadString).post(formBody).build();
call = client.build().newCall(request);
call.enqueue(new Callback() {
//失败的回调
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
if (m_pDialog.isShowing()) {
AlertText("网络连接超时,请检查您的网络连接。");
}
}
//成功的回调
@Override
public void onResponse(Call call, okhttp3.Response response) throws IOException {
InputStream is = null;
FileOutputStream fos = null;
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
if (responseHeaders.name(i).equals("Content-Disposition")) {
fileName = new String(responseHeaders.value(i)).substring(21, responseHeaders.value(i).length());
}
}
try {
is = response.body().byteStream();
String dir = Environment.getExternalStorageDirectory()
+ "/sss/";
if (FileUtil.makeFolder(dir)) {
File file = new File(dir, fileName);
fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
while ((ch = is.read(buf)) != -1) {
fos.write(buf, 0, ch);
}
fos.flush();
if (fos != null) {
fos.close();
}
OpenFile();
} else {
AlertText("程序自动试图创建文件夹失败");
}
} catch (Exception e) {
e.printStackTrace();
if (m_pDialog.isShowing()) {
AlertText("您所下载的内容不存在。");
}
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
OkHttp3 使用的更多相关文章
- Okhttp3的简单使用
1.get请求: /** * *okhttp get请求 * */ public class MainActivity extends AppCompatActivity { private stat ...
- okhttp3 post 数据打包方法
import okhttp3.OkHttpClient; import okhttp3.FormBody; import okhttp3.Request; import okhttp3.Request ...
- Retrofit2 + OkHttp3设置Http请求头(Headers)方法汇总
在构建网络层时会遇到一个问题就是要手动配置Http请求的Headers,写入缓存Cookie,自定义的User-Agent等参数,但是对于有几十个接口的网络层,我才不想用注解配置Headers,目前网 ...
- okhttp3 get post 简单封装
最近打算在新项目中使用 okhttp3, 简单封装了一下异步 get post 因为 CallBack 也是在子线程中执行,所以用到了 Handler public class MyOkHttpCli ...
- okhttp3教程(1)如何引入库
官网: https://github.com/square/okhttp https://github.com/square/okio 1,使用okhttp3需要两个库 在build.gradle c ...
- Android网络框架---OkHttp3
1.添加依赖 compile 'com.squareup.okhttp3:okhttp:3.4.2' project Structure-->dependencied/搜索okhttp. com ...
- retrofit2 okhttp3 RxJava butterknife 示例
eclipse的jar包配置 eclipse中貌似用不了butterknife buildToolsVersion "23.0.2" defaultConfig { applica ...
- Rxjava+Retrofit2+Okhttp3多文件上传(服务器端代码+客户端代码)
所有代码亲测可用,如有问题,欢迎指正. 首先在ApiService接口文件中新建文件上传接口 public interface ApiService { static final String BAS ...
- Okhttp3日志采集功能
原文地址以示尊重:http://www.jianshu.com/p/d836271b1ae4 日志采集是一个APP必备的功能,可以方便开发人员快速定位问题,解决问题,那么我们在使用okhttp的时候应 ...
- Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava
Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava Retrofit,因为其简单与出色的性能,也是受到很多人的青睐,但是他和以往的通信 ...
随机推荐
- [linux]centos7.4部署django+Uwsgi+Nginx
前言:我已经写了几个接口用来部署在服务器上的,首先选择django+Uwsgi+Nginx因为配置简单,比较符合python的简单操作功能强大的特点 然后对于django的一些版本在之前的文章写了 参 ...
- SpringCloud升级之路2020.0.x版-33. 实现重试、断路器以及线程隔离源码
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在前面两节,我们梳理了实现 Feign 断路器以及线程隔离的思路,并说明了如何优化目前的负 ...
- Part 15 AngularJS ng init directive
The ng-init directive allows you to evaluate an expression in the current scope. In the following e ...
- 偷天换日,用JavaAgent欺骗你的JVM
原创:微信公众号 码农参上(ID:CODER_SANJYOU),欢迎分享,转载请保留出处. 熟悉Spring的小伙伴们应该都对aop比较了解,面向切面编程允许我们在目标方法的前后织入想要执行的逻辑,而 ...
- [cf1219G]Harvester
分类讨论(以下仅考虑行,列的情况):1.4行的,求出每一行的和后找到4个最大值即可:2.3行1列,枚举列,再将每一行最大值减去那一列的值后取3个最大值得和即可:3.2行2列,发现行和列是等价的,因此可 ...
- [noi1994]海盗
令$a_{i,j}(j\le i)$表示第i个人的方案中给第j个人$a_{i,j}$的钱,有以下性质: 1.如果第j个人一定同意(否则就会死)第i个人的方案,那么$a_{i,j}=0$(容易发现一定同 ...
- *(volatile unsigned int *)的理解
1. 解释 前面是无符号整型unsigned int的指针, 后面加一个地址,就是无符号整型的地址,前面又一个星号就是这个地址的值. 2.volatile 同步 因为同一个东西可能在不同的存储介质中有 ...
- Go语言核心36讲(Go语言实战与应用十二)--学习笔记
34 | 并发安全字典sync.Map (上) 我们今天再来讲一个并发安全的高级数据结构:sync.Map.众所周知,Go 语言自带的字典类型map并不是并发安全的. 前导知识:并发安全字典诞生史 换 ...
- docker 启动报错:Docker.Core.Backend.BackendException: Error response from daemon: open \\.\pipe\docker_e
win10 docker启动后报错: Docker.Core.Backend.BackendException:Error response from daemon: open \\.\pipe\do ...
- SimpleNVR安防监控RTSP/FLV/HLS直播流服务如何分权限添加用户指定通道观看
背景分析 随着SimpleNVR的用户越来越多,很多客户反馈给了我们很宝贵的简易以及用户体验.在此非常感谢大家对我们的支持.其中很多客户不想把所有的视频直播展现出来,想分权限添加新用户,指定通道让其观 ...