OkHttp简介
什么是OKHttp
一般在Java平台上,我们会使用Apache HttpClient作为Http客户端,用于发送 HTTP 请求,并对响应进行处理。比如可以使用http客户端与第三方服务(如SSO服务)进行集成,当然还可以爬取网上的数据等。OKHttp与HttpClient类似,也是一个Http客户端,提供了对 HTTP/2 和 SPDY 的支持,并提供了连接池,GZIP 压缩和 HTTP 响应缓存功能;
官网:http://square.github.io/okhttp/
添加依赖
在Java中使用OKHttp很简单,如果是maven工程,往pom.xml添加如下xml片段即可,目前最新版本2.7.5
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
如果是gradle,添加如下依赖
compile group: 'com.squareup.okhttp', name: 'okhttp', version: '2.7.5'
简单使用
后端Controller
一个简单的spring mvc web应用。
@RequestMapping(value = "/getUserList", produces = "application/json; charset=utf-8")
@ResponseBody
public String getUserList(int pageNo, int pageSize)
{
Map<String, Object> map = new HashMap<String, Object>();
try
{
Map<String, Object> param = new HashMap<String, Object>();
param.put("pageNo", pageNo);
param.put("pageSize", pageSize);
List<User> userList = userService.queryAll(param);
map.put("userList", userList);
return gson.toJson(map);
}
catch (Exception e)
{
logger.error(e.toString(), e);
}
return gson.toJson(FAILD);
}
使用OkHttp发送Ge请求
package cn.hdu.edu.okhttpdemo; import java.io.IOException; import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response; /**
* Hello world!
*
*/
public class App
{
public String run(OkHttpClient client, String url) throws IOException {
Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute();
return response.body().string();
}
public static void main( String[] args )
{
OkHttpClient client = new OkHttpClient();
try
{
String res = new App().run(client, "http://localhost:8080/webbf/user/getUserList.do?pageNo=0&pageSize=10");
System.out.println(res);
}
catch (IOException e)
{
e.printStackTrace();
} }
}
使用OkHttp发送Post请求
package cn.hdu.edu.okhttpdemo; import java.io.IOException; import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response; /**
* Hello world!
*
*/
public class App
{
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); public String post(OkHttpClient client, String url) throws IOException
{
Request request = new Request.Builder()
.url(url)
.post(new FormEncodingBuilder()
.add("pageNo", "0") //参数1
.add("pageSize", "10") //参数二
.build())
.build();
Response response = client.newCall(request).execute();
return response.body().string();
} public static void main(String[] args)
{
OkHttpClient client = new OkHttpClient();
try
{
String res = new App().post(client,
"http://localhost:8080/webbf/user/getUserList.do");
System.out.println(res);
}
catch (IOException e)
{
e.printStackTrace();
} }
}
结果打印
请求成功,打印以下数据:
{"userList":[{"id":49,"name":"876","address":"876"},{"id":50,"name":"antd","address":"antd"},{"id":51,"name":"sda","address":"sadsd"},{"id":52,"name":"5545","address":"4646546546"},{"id":53,"name":"sdas","address":"sdasa"},{"id":54,"name":"hggs","address":"sdsd"},{"id":55,"name":"4","address":"5"},{"id":56,"name":"4","address":"4"},{"id":57,"name":"00ba9d8e-0628-4477-857f-ef617c1ff4bc","address":"5906"},{"id":58,"name":"613ee3a3-fb87-4413-a8e0-9272d10ad4a7","address":"6427"}]}
OkHttp简介的更多相关文章
- Okhttp【简介】应用 示例
资源 GitHub:https://github.com/square/okhttp 官网 文档 API You'll also need Okio[https://github.c ...
- OkHttp:Java 平台上的新一代 HTTP 客户端
OkHttp 简介 OkHttp 库的设计和实现的首要目标是高效.这也是选择 OkHttp 的重要理由之一.OkHttp 提供了对最新的 HTTP 协议版本 HTTP/2 和 SPDY 的支持,这使得 ...
- Android之OkHttp详解
文章大纲 一.OkHttp简介二.OkHttp简单使用三.OkHttp封装四.项目源码下载 一.OkHttp简介 1. 什么是OkHttp 一般在Java平台上,我们会使用Apache Htt ...
- 【转】Android OkHttp3简介和使用详解
一 OKHttp简介 OKHttp是一个处理网络请求的开源项目,Android 当前最火热网络框架,由移动支付Square公司贡献,用于替代HttpUrlConnection和Apache HttpC ...
- Android OkHttp3简介和使用详解
一 OKHttp简介 OKHttp是一个处理网络请求的开源项目,Android 当前最火热网络框架,由移动支付Square公司贡献,用于替代HttpUrlConnection和Apache HttpC ...
- Android 问题列表
25. Touch 事件传递机制 26. 点击事件设置监听的几种方式 27. Hander 的原理 28. Thread 和HandThread 的区别 29. AsyncTask 简介 30. As ...
- SpringCloud设定Feign底层实现
1. 概述 版本: spring-boot: 1.5.9.RELEASE spring-cloud: Dalston.SR5 在默认情况下 spring cloud feign在进行各个子服务之间的 ...
- Picasso的使用
相信做Android开发的对Square公司一定不会陌生,大名鼎鼎的网络请求框架Retrofit就来源于它,今天学习的是该公司出品的图片加载框架Picasso. 项目地址 https://github ...
- Android图片加载框架之Picasso
相信做Android开发的对Square公司一定不会陌生,大名鼎鼎的网络请求框架Retrofit就来源于它,今天学习的是该公司出品的图片加载框架Picasso. 项目地址 https://github ...
随机推荐
- publishing failed with multiple errors resource is out of sync with the file system--转
原文地址:http://blog.csdn.net/feng1603/article/details/7398266 今天用eclipse部署项目遇到"publishing failed w ...
- 记录一则ORA-00054,ORA-00031解决过程
生产环境:AIX 5.3 + Oracle 10.2.0.5 任务要求:普通表改造分区表,历史数据不要 这个需求很简单: pl/sql导出建表语句,依次修改成分区的建表语句,注意将索引修改成本地索 ...
- opendaylight的Beryllium安装
1.首先安装jdk #sudo apt-get install openjdk-7-jdk 2.安装vim编辑工具 #sudo apt-get install vim 3.编辑~/.bashrc ...
- AngularJs之Scope作用域
前言: 上篇博文AngularJs之directive中说了Scope作用域是个大坑,所以拿出来作为重点总结! 什么是scope AngularJS 中,作用域是一个指向应用模型的对象,它是表达式的执 ...
- jquery动画,基础以及我发现的新大陆
$.animate()在jquery官方介绍有2中方式,其实我发现的新大陆也是第二种方式的扩展! 一.$.animate( properties [, duration ] [, easing ] [ ...
- 测试为什么Low
你从来没有因为一个歌手不会写曲填词而说歌手很Low! 你从来没有因为一个演员不会摄影.唱歌而说演员很Low! 你从来没有因为一个记者不会摄影,拍照而说记者很Low! 你从来没有因为一个美食家不会烧菜, ...
- Visual Studio中安装viemu后,vim vax 快捷键大全
高效率移动 在插入模式之外 基本上来说,你应该尽可能少的呆在插入模式里面,因为在插入模式里面VIM就像一个“哑巴”编辑器一样.很多新手都会一直呆在插入模式里面,因为这样易于使用.但VIM的强大之处在于 ...
- vim使用心得(持续更新)
!统计符合条件的行数方法1 :g/<匹配条件>/d 查看影响行数,然后按u恢复. !统计符合条件的行数方法2 :let numb=0 :g/<匹配条件>/let numb+ ...
- 引用Microsoft.Office.Interop.Excel出现的问题
引用Microsoft.Office.Interop.Excel出现的问题 转自:http://www.hccar.com/Content,2008,6,11,75.aspx,作者:方继祥 操作背 ...
- 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合
[源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...