* OKHttp3
HttpURLConnection特点:HttpURLConnection是一种多用途、轻量极的HTTP客户端,使用它来进行HTTP操作可以适用于大多数的应用程序。虽然HttpURLConnection的API提供的比较简单,但是同时这也使得我们可以更加容易地去使用和扩展它。从Android4.4开始HttpURLConnection的底层实现采用的是okHttp。
实现了一个网络连接的过程。所以按照层级来说,OkHttp和HttpUrlConnection是一级的,用socket实现了网络连接,只是OkHttp更强大;
支持HTTP2/SPDY黑科技
socket自动选择最好路线,并支持自动重连
拥有自动维护的socket连接池,减少握手次数
拥有队列线程池,轻松写并发
拥有Interceptors轻松处理请求与响应(比如透明GZIP压缩,LOGGING)
基于Headers的缓存策略
* 如何使用:
* 1:配置网络权限* 2:导入依赖
* 3:写一些代码
* 同步和异步请求网络
* 1:同步是耗时操作必须要放在子线程里 ,异步不需要
//1)同步GET请求
//1:需要一个OKHTTPClient对象
OkHttpClient okHttpClient = new OkHttpClient();
//2:封装请求数据
Request request = new Request.Builder().url(mUrl).build();
//3:发送请求并且生成一个相应
Response response = okHttpClient.newCall(request).execute();
//3:得到相应体里面数据
String string = response.body().string();
//2)同步请求POST发送方式
OkHttpClient ok = new OkHttpClient();
//请求体参数
FormBody formBody = new FormBody.Builder().add("userName", "lxx").add("passWord", "123").build();
//设置请求方式
Request request = new Request.Builder().url(mPostUrl).post(formBody).build();
Response response = ok.newCall(request).execute();
//1)异步GET请求
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(mUrl).build();
okHttpClient.newCall(request).enqueue(new Callback() {
//这个接口访问失败时做的操作
@Override
public void onFailure(Call call, IOException e) {
}
//一个成功时做的操作
@Override
public void onResponse(Call call, Response response) throws IOException {
}
//2)异步POST请求
OkHttpClient okHttpClient = new OkHttpClient();
FormBody formBody = new FormBody.Builder().add("userName", "lxs").add("passWord", "123").build();
Request request = new Request.Builder().url(mPostUrl).post(formBody).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("异步Post请求", response.body().string());
}
/**
* Created by lvxinxin on 2017/12/19.
* OKHTTP工具类
* 加入缓存
*/
public class MyOkHttp {
private static MyOkHttp myOkHttp = new MyOkHttp();
private OkHttpClient okHttpClient;
private Cache cache;
private long maxSize = 8 * 1024 * 1024;
private MyOkHttp() {
//Environment用来管理手机SDcard内存的
File file = new File(Environment.getExternalStorageDirectory() + "/H1706A");
cache = new Cache(file, maxSize);
//OKHTTPClient两种写法 第二种就是通过构造者模式也可以使用
okHttpClient = new OkHttpClient.Builder().cache(cache).build();
}
public static MyOkHttp getMyOkHttp() {
return myOkHttp;
}
//同步下get,Post
public String sendGet(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
}
//同步下Post
public String sendPost(String url, RequestBody body) throws IOException {
Request request = new Request.Builder().url(url).post(body).build();
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
}
public void sendAsyncGet(String url, Callback callback) {
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(callback);
}
//异步的POST
public void sendAsyncPost(String url, RequestBody body, Callback callback) {
Request request = new Request.Builder().url(url).post(body).build();
okHttpClient.newCall(request).enqueue(callback);
}
//测试缓存
public String sendCacheGet(String url) throws IOException {
//要缓存数据 需要加上一个方法cacheControl
Request request = new Request.Builder().url(url).cacheControl(new CacheControl.Builder().maxStale(24 * 60 * 60, TimeUnit.SECONDS).build()).build();
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
}
}