OkHttp的简单使用
来源:互联网 发布:zeppelin源码下载 编辑:程序博客网 时间:2024/05/20 03:05
OkHttp3.0
加入依赖
一:get请求
无非就是3步走
1.创建客户端
2.创建请求
3.发送请求,处理响应
同步Get请求
//创建客户端 OkHttpClient client = new OkHttpClient(); //创建get请求 Request request = new Request.Builder() .get() .url("http:www.123.com") .build(); try { //客户端发送请求,得到响应 Response response = client.newCall(request).execute(); if(!response.isSuccessful()){ return; } String result = response.body().string(); } catch (IOException e) { e.printStackTrace(); }
异步Get请求
OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //响应成功 String result = response.body().string(); //解析json } });
OkHttpClient client = new OkHttpClient(); //创建一个请求体 FormBody body = new FormBody.Builder() .add("key1","value1") .add("key2","value2") .build(); Request request = new Request.Builder() .url("") .post(body) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //还可以进行流的对接,读写 String data = response.body().string(); InputStream in = response.body().byteStream(); ... } });
0 0
- OkHttp的简单使用
- OKHttp的简单使用
- OKHttp的简单使用
- OkHttp的简单使用
- okhttp的简单使用
- OkHttp的简单使用
- OkHttp的简单使用
- OkHttp的简单使用
- OkHttp的简单使用
- OkHttp 的简单使用
- okhttp的简单使用
- Okhttp的简单使用
- OKhttp的简单使用
- 简单的OkHttp使用介绍
- OkHttp框架的简单使用
- OkhttP的简单使用笔记
- Android okHttp的简单使用
- okhttp的简单使用例子
- 人生的意义
- C语言好题&错题笔记
- 《谈修养》
- JBorder组件边框
- 机器学习与R之KNN
- OkHttp的简单使用
- github命令
- JTable 为单元格添加按钮效果和事件效果
- C#依赖注入实例解析
- Linux 内核空间虚拟地址和物理地址相互转换
- easyui datagrid综合功能demo实现
- maven+springmvc+mybatis项目从properties读数据库连接属性时拿不到连接,的研究与解决方案
- python-scipy 图像处理
- listView的优化