An HTTP & HTTP/2 client for Android and Java applications OkHttp
HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth.
OkHttp is an HTTP client that’s efficient by default:
- HTTP/2 support allows all requests to the same host to share a socket.
- Connection pooling reduces request latency (if HTTP/2 isn’t available).
- Transparent GZIP shrinks download sizes.
- Response caching avoids the network completely for repeat requests.
OkHttp perseveres when the network is troublesome: it will silently recover from common connection problems. If your service has multiple IP addresses OkHttp will attempt alternate addresses if the first connect fails. This is necessary for IPv4+IPv6 and for services hosted in redundant data centers. OkHttp initiates new connections with modern TLS features (SNI, ALPN), and falls back to TLS 1.0 if the handshake fails.
Using OkHttp is easy. Its request/response API is designed with fluent builders and immutability. It supports both synchronous blocking calls and async calls with callbacks.
OkHttp supports Android 2.3 and above. For Java, the minimum requirement is 1.7.
http://square.github.io/okhttp/
GET A URL
This program downloads a URL and print its contents as a string.
- package okhttp3.guide;
- import java.io.IOException;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.Response;
- public class GetExample {
- OkHttpClient client = new OkHttpClient();
- String run(String url) throws IOException {
- Request request = new Request.Builder()
- .url(url)
- .build();
- try (Response response = client.newCall(request).execute()) {
- return response.body().string();
- }
- }
- public static void main(String[] args) throws IOException {
- GetExample example = new GetExample();
- String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
- System.out.println(response);
- }
- }
POST TO A SERVER
This program posts data to a service.
- package okhttp3.guide;
- import java.io.IOException;
- import okhttp3.MediaType;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.RequestBody;
- import okhttp3.Response;
- public class PostExample {
- public static final MediaType JSON
- = MediaType.parse("application/json; charset=utf-8");
- OkHttpClient client = new OkHttpClient();
- String post(String url, String json) throws IOException {
- RequestBody body = RequestBody.create(JSON, json);
- Request request = new Request.Builder()
- .url(url)
- .post(body)
- .build();
- try (Response response = client.newCall(request).execute()) {
- return response.body().string();
- }
- }
- 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 {
- PostExample example = new PostExample();
- String json = example.bowlingJson("Jesse", "Jake");
- String response = example.post("http://www.roundsapp.com/post", json);
- System.out.println(response);
- }
- }
An HTTP & HTTP/2 client for Android and Java applications OkHttp的更多相关文章
- 如何使用GraphQL Client: Apollo Android
如何使用GraphQL Client: Apollo Android 一个Android app, 如何使用GraphQL. 本文以最流行的Apollo Android为例来说明. 添加依赖 首先, ...
- Android使用RxJava+Retrofit2+Okhttp+MVP练习的APP
Android使用RxJava+Retrofit2+Okhttp+MVP练习的APP 项目截图 这是我的目录结构 五步使用RxJava+Retrofit2+Okhttp+RxCache 第一步 ...
- Android(或者Java)通过HttpUrlConnection向SpringMVC请求数据(数据绑定)
问题描写叙述 当我们使用SpringMVC作为服务端的框架时,有时不仅仅要应对web前端(jsp.javascript.Jquery等)的訪问请求,有时还可能须要响应Android和JavaSE(桌面 ...
- [转载] 深入理解Android之Java虚拟机Dalvik
本文转载自: http://blog.csdn.net/innost/article/details/50377905 一.背景 这个选题很大,但并不是一开始就有这么高大上的追求.最初之时,只是源于对 ...
- Android(java)学习笔记267:Android线程池形态
1. 线程池简介 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为:T1 创建线程时间, ...
- Android中Java反射技术的使用示例
import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Metho ...
- Android(java)学习笔记207:开源项目使用之gif view
1. 由于android没有自带的gif动画,我在Android(java)学习笔记198:Android下的帧动画(Drawable Animation) 播客中提到可以使用AnimationVie ...
- Android(java)学习笔记71:生产者和消费者之等待唤醒机制
1. 首先我们根据梳理我们之前Android(java)学习笔记70中关于生产者和消费者程序思路: 2. 下面我们就要重点介绍这个等待唤醒机制: (1)第一步:还是先通过代码体现出等待唤醒机制 pac ...
- Android 虚拟机Dalvik、Android各种java包功能、Android相关文件类型、应用程序结构分析、ADB
Android虚拟机Dalvik Dalvik冲击 随着Google 的AndroidSDK 的发布,关于它的API 以及在移动电话领域所带来的预期影响这些方面的讨论不胜枚举.不过,其中的一个话题在J ...
随机推荐
- Swift iOS tableView static cell动态计算高度
TableView是iOS开发中经常使用的组件.有些表格由于UILabel包括的文本字数不一样,须要显示的高度也会不同,因此须要动态计算static cell的高度.我用的是static cell,注 ...
- 快速搭建REST API——json server
一:全局安装json-server npm install json-server -g 二:在自己项目跟目录下存放mock/data.json,json内容如下: { "roles&quo ...
- GPUImage ==> 一个基于GPU图像和视频处理的开源iOS框架
Logo 项目介绍: GPUImage是Brad Larson在github托管的开源项目. GPUImage是一个基于GPU图像和视频处理的开源iOS框架,提供各种各样的图像处理滤镜,并且支持照相机 ...
- Docker---(2)docker pull 下来的镜像存储在哪里
原文:Docker---(2)docker pull 下来的镜像存储在哪里 版权声明:欢迎转载,请标明出处,如有问题,欢迎指正!谢谢!微信:w1186355422 https://blog.csdn. ...
- C#学习笔记——常量、字段以及事件
一 常量与字段 (一) 常量 常量总是被视为静态成员,而不是实例成员.定义常量将导致创建元数据.代码引用一个常量时,编译器会在定义常量的程序集的元数据中查找该符号,提取常量的值,并将值嵌入IL中.由于 ...
- Oracle 12CR2 中alert.log出现大量的 WARNING: too many parse errors 告警
Oracle 12CR2 中alert.log出现大量的 WARNING: too many parse errors 告警 日志如下: 2018-06-24T17:16:21.024586+08 ...
- JS数据结构第二篇---链表
一.什么是链表 链表是一种链式存储的线性表,是由一组节点组成的集合,每一个节点都存储了下一个节点的地址:指向另一个节点的引用叫链:和数组中的元素内存地址是连续的相比,链表中的所有元素的内存地址不一定是 ...
- stm32的dma缓冲区长度,,存放数据数组会不会冲掉
- 一致哈希算法Java实现
一致哈希算法(Consistent Hashing Algorithms)是一个分布式系统中经常使用的算法. 传统的Hash算法当槽位(Slot)增减时,面临全部数据又一次部署的问题.而一致哈希算法确 ...
- MySql的事务操作与演示样例
事务就是一个逻辑工作单元的一系列步骤. 事务是用来保证数据操作的安全性 事务的特征: Atomicity(原子性) Consistency(稳定性,一致性) Isolation(隔离性) Durabi ...