关于OkHttp–支持SPDY协议的高效HTTP库 com.squareup.okhttp
转载:http://liuzhichao.com/p/1707.html
OkHttp–支持SPDY协议的高效HTTP库
Android为我们提供了两种HTTP交互的方式: HttpURLConnection 和 Apache HTTP Client,虽然两者都支持HTTPS,流的上传和下载,配置超时,IPv6和连接池,已足够满足我们各种HTTP请求的需求。但更高效的使用HTTP可以让您的应用运行更快、更节省流量。而OkHttp库就是为此而生。
OkHttp是一个高效的HTTP库:
- 支持 SPDY ,共享同一个Socket来处理同一个服务器的所有请求
- 如果SPDY不可用,则通过连接池来减少请求延时
- 无缝的支持GZIP来减少数据流量
- 缓存响应数据来减少重复的网络请求
会从很多常用的连接问题中自动恢复。如果您的服务器配置了多个IP地址,当第一个IP连接失败的时候,OkHttp会自动尝试下一个IP。OkHttp还处理了代理服务器问题和SSL握手失败问题。
使用 OkHttp 无需重写您程序中的网络代码。OkHttp实现了几乎和java.net.HttpURLConnection一样的API。如果您用了 Apache HttpClient,则OkHttp也提供了一个对应的okhttp-apache 模块。
Examples
下面的示例请求一个URL并答应出返回内容字符.
package com.squareup.okhttp.guide; import com.squareup.okhttp.OkHttpClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class GetExample {
OkHttpClient client = new OkHttpClient(); void run() throws IOException {
String result = get(new URL("https://raw.github.com/square/okhttp/master/README.md"));
System.out.println(result);
} String get(URL url) throws IOException {
HttpURLConnection connection = client.open(url);
InputStream in = null;
try {
// Read the response.
in = connection.getInputStream();
byte[] response = readFully(in);
return new String(response, "UTF-8");
} finally {
if (in != null) in.close();
}
} byte[] readFully(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int count; (count = in.read(buffer)) != -1; ) {
out.write(buffer, 0, count);
}
return out.toByteArray();
} public static void main(String[] args) throws IOException {
new GetExample().run();
}
}
下面的代码通过Post发送数据到服务器:
package com.squareup.okhttp.guide; import com.squareup.okhttp.OkHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class PostExample {
OkHttpClient client = new OkHttpClient(); void run() throws IOException {
byte[] body = bowlingJson("Jesse", "Jake").getBytes("UTF-8");
String result = post(new URL("http://www.roundsapp.com/post"), body);
System.out.println(result);
} String post(URL url, byte[] body) throws IOException {
HttpURLConnection connection = client.open(url);
OutputStream out = null;
InputStream in = null;
try {
// Write the request.
connection.setRequestMethod("POST");
out = connection.getOutputStream();
out.write(body);
out.close(); // Read the response.
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException("Unexpected HTTP response: "
+ connection.getResponseCode() + " " + connection.getResponseMessage());
}
in = connection.getInputStream();
return readFirstLine(in);
} finally {
// Clean up.
if (out != null) out.close();
if (in != null) in.close();
}
} String readFirstLine(InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
return reader.readLine();
} String bowlingJson(String player1, String player2) {
return "{'winCondition':'HIGH_SCORE',"
+ "'name':'Bowling',"
+ "'round':4,"
+ "'lastSaved':1367702411696,"
+ "'dateStarted':1367702378785,"
+ "'players':["
+ "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39},"
+ "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}"
+ "]}";
} public static void main(String[] args) throws IOException {
new PostExample().run();
}
}
参考:
http://square.github.io/okhttp/
http://android-developers.blogspot.com/2011/09/androids-http-clients.html
关于OkHttp–支持SPDY协议的高效HTTP库 com.squareup.okhttp的更多相关文章
- OkHttp–支持SPDY协议的高效HTTP库
Android为我们提供了两种HTTP交互的方式: HttpURLConnection 和 Apache HTTP Client,虽然两者都支持HTTPS,流的上传和下载,配置超时,IPv6和连接池, ...
- Visual Studio 2015 编译生成支持HTTPS协议的libcurl静态库
由于之前的工作需要使用libcurl 开源项目库 在各种研究后发现无法使用HTTPS协议 后来经过各种翻阅文档,发现需要OpenSSL支持,这个需要自己下载并自己编译生成 lib 或者 dll 至于O ...
- HTTP代理与SPDY协议(转)
原文出处: fqrouter HTTP代理是最经典最常见的代理协议.其用途非常广泛,普遍见于公司内网环境,一般员工都需要给浏览器配置一个HTTP代理才能访问互联网.起初,HTTP代理也用来翻越“功夫网 ...
- Ubuntu 12.04 LTS 下配置 apache支持SPDY, 使用wireshark 抓包分析SPDY 协议
1.安装apache sudo apt-get install apache2 root@ubuntu:/etc/apache2/mods-enabled# apache2 -v Server ver ...
- 【转】SPDY协议
SPDY协议 - v3 原文:SPDY Protocol - Draft 3 翻译:邱鹏滔(QQ: 95350530,主页:www.fireflysource.com) 1 概述 HTTP协议的瓶颈在 ...
- Netty 系列九(支持UDP协议).
一.基础知识 UDP 协议相较于 TCP 协议的特点: 1.无连接协议,没有持久化连接:2.每个 UDP 数据报都是一个单独的传输单元:3.一定的数据报丢失:4.没有重传机制,也不管数据报是否可达:5 ...
- 【Spdy协议简介】
一.SPDY协议诞生记 SPDY (SPDY 是 Speedy 的昵音,意思是更快)是 Google 开发的基于传输控制协议 (TCP) 的应用层协议 ,那么为什么要搞一个SPDY出来呢?距离万维网之 ...
- nginx支持http2协议
1.http2协议 HTTP 2.0 的主要目标是改进传输性能,实现低延迟和高吞吐量.从另一方面看,HTTP 的高层协议语义并不会因为这次版本升级而受影响.所有HTTP 首部.值,以及它们的使用场景都 ...
- 阿里云全站加速DCDN全面支持WebSocket协议
WebSocket协议可以为网站和应用提供真正的双向通信,具有控制开销.保持连接状态.更强实时性.更好的压缩效果等优点,是当下低延时应用最常采用的一种技术协议.为了更好的满足客户在实时通讯场景下的加速 ...
随机推荐
- Migrating from Spring 3 to Spring 4 - org.springframework.scheduling.quartz.CronTriggerBean
I'm trying to migrate from spring 3.0.5 to spring 4.1.X . Spring 3 has Class named as "org.spri ...
- 解决com.mongodb.MongoException$CursorNotFound: cursor 0 not found on server
背景 经常需要执行脚本调用Java程序读取mongodb中数据,本来是转为后台进程.偶尔看看日志的简单任务.今天发现程序抛出异常“com.mongodb.MongoException$CursorNo ...
- 如何表示各个时区的时间DateTime.ToString()
使用sqlite进行时间的插入的时候,使用了 DateTime.ToString("s") //s: 2008-06-15T21:15:07 插入到数据库之后,发现时间被加了8个 ...
- bootstrap.min.css.map HTTP/1.1" 404 1699
在做一个jsp练习的时候遇到引入bootstrap.css的时候出现了URL:bootstrap.min.css.map 404的错误. 解决办法:删除bootstrap.min.css文件内容最后一 ...
- PHP 手机号中间4位加密
/** * 中间加密 字符串截取法 */ public static function encryptTel($tel) { $new_tel = substr($tel, 0, 3).'****'. ...
- PythonInstaller编译EXE方法+编译过程出错方案大全
https://www.cnblogs.com/gopythoner/p/6337543.htmlhttps://www.zhihu.com/question/22963200https://blog ...
- ssh隧道(通过跳板机)连接mysql
案例: A服务器 B服务器 C服务器mysql 现在mysql服务器C只能通过内网访问,B服务器就能通过内网连接访问到mysql A服务器无法直接连接C服务器mysql,所以要通过跳板机(跳板 ...
- 记c#中构造函数中this的用法
今天谈及这个话题,是因为在看别人代码,遇到的.本着知其然还要知其所以然的精神,在网上大肆查找了一遍.得到了一些答案.并在此做一个简单的笔记,以便自己和他人以后查找,与我一同成长进步. 在C#中this ...
- New Concept English Two 30 82
$课文80 水晶宫 867. Perhaps the most extraordinary building of the nineteeth century was the Crystal Pal ...
- java 注解总结
@Controller用于标注控制层组件 @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象.分发处理器将会扫描使用了该注解的类的方法.通 ...