一、注意事项html
使用的jar为okio-1.9.0.jar和okhttp-3.8.0.jar;java
若是okio架包偏低就会报错;okhttp架包版本太低也会出现架包冲突;android
二、网上的例子:git
private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { Request request = new Request.Builder() .url("http://publicobject.com/helloworld.txt") .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Headers responseHeaders = response.headers(); for (int i = 0; i < responseHeaders.size(); i++) { System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); } System.out.println(response.body().string()); }
三、利用多线程以及CountDownLatch实现利用在后台实现多线程http请求:github
OkHttpClient httpClient=new OkHttpClient(); Request request=new Request.Builder() .url(url) .build(); //获得response对象 Response response=httpClient.newCall(request).execute(); if(response.isSuccessful()){ String result=response.body().string(); System.out.println(response.code()); System.out.println(response.message()); System.out.println(result); }
使用main方法进行入口测试:web
public static void main(String[] args) { CountDownLatch latch=new CountDownLatch(1); String url="http://test.meyki.net:9080/apidev/v3/goods/goodsList/eyJwYWdlU2l6ZSI6IjIwIiwib3MiOiJpb3MiLCJzaG9wSWQiOiI1MTU2MjNmNGVlOGU0OGIxYmU3MmY2MGFiOTcxZTExOSIsInVzZXJJZCI6IjIyNTg0MDVmYTU5ODRlOTViNWI2NTI4ZTM3YmExODBjIiwicGFnZU5vIjoiMSIsImlzU2hlbHZlcyI6IjEiLCJ2Y29kZSI6IjE0MCIsInRhZyI6IjEifQ=="; for(int i=0;i<=15;i++){ Thread thread=new Thread(new Mythread(latch,url)); thread.start(); } latch.countDown(); }
详解:算法
2.1 OkHttp计划使用三种类型链接到你的web服务器:URL, Address, 和 Route。apache
URLs(如https://github.com/square/okhttp)是HTTP和因特网的基础。除了是网络上通用和分散的命名方案,他们还指定了如何访问网络资源。json
他们还具体:每一个URL识别特定的路径(如 /square/okhttp)和查询(如 ?q=sharks&lang=en)。每一个Web服务器主机的网址。api
Addresses指定网络服务器(如github.com)和全部的静态必要的配置,以及链接到该服务器:端口号,HTTPS设置和首选的网络协议(如HTTP / 2或SPDY)。
共享相同地址的URL也能够共享相同的基础TCP套接字链接。共享一个链接有实实在在的性能优势:更低的延迟,更高的吞吐量(因为TCP慢启动)和保养电池。OkHttp使用的ConnectionPool自动重用HTTP / 1.x的链接和多样的HTTP/ 2和SPDY链接。
在OkHttp地址的某些字段来自URL(scheme, hostname, port),其他来自OkHttpClient。
Routes提供链接到一个网络服务器所必需的动态信息。就是尝试特定的IP地址(如由DNS查询发现),使用确切的代理服务器(若是一个特定的IP地址的ProxySelector在使用中)和协商的TLS版本(HTTPS链接)。
可能有单个地址对应多个路由。例如,在多个数据中心托管的Web服务器,它可能会在其DNS响应产生多个IP地址。
当你使用OkHttp进行一个URL请求时,下面是它的操做流程:
一旦响应已经被接收到,该链接将被返回到池中,以便它能够在未来的请求中被重用。链接在池中闲置一段时间后,它会被赶出。
典型的HTTP头工做就像一个Map<String, String> :每一个字段都有一个值或无值。可是,一些头部(headers)容许多个值,好比Guava的Multimap。例如,它共同为一个HTTP响应提供多个Vary头。OkHttp的API,试图使这两种状况下都能温馨使用。
当写请求头,用header(name, value)来为惟一出现的name设置value。若是它自己存在值,在添加新的value以前,他们会被移除。使用addHeader(name, value)来添加头部不须要移除当前存在的headers。
当读取响应头,用header(name)返回最后设置name的value。若是没有value,header(name)将返回null。可使用headers(name)来读取全部列表字段的值,。
要访问全部的头部,用Headers类,它支持索引访问。
private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { Request request = new Request.Builder() .url("https://api.github.com/repos/square/okhttp/issues") .header("User-Agent", "OkHttp Headers.java") .addHeader("Accept", "application/json; q=0.5") .addHeader("Accept", "application/vnd.github.v3+json") .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println("Server: " + response.header("Server")); System.out.println("Date: " + response.header("Date")); System.out.println("Vary: " + response.headers("Vary")); }
OkHttpClient httpClient=new OkHttpClient();
在建立不一样的httpClient方法实现不一样的操做;详细请查看:
https://blog.csdn.net/jackingzheng/article/details/51778793#
关注公众号,回复c+兴趣的东西 24小时内便可领取学习。2T资料任君挑选!