一、概述

OKHttp是一个十分常用的网络请求框架了,所以一定要学习它的使用,更甚者还需要阅读它的源码和了解实现逻辑。OKHttp官网,OKHttp源码

1、优点如下

网络优化方面:

(1)内置连接池,支持连接复用;

(2)支持gzip压缩响应体;

(3)通过缓存避免重复的请求;

(4)支持http2,对一台机器的所有请求共享同一个socket。

功能方面:

功能全面,满足了网络请求的大部分需求

扩展性方面:

责任链模式使得很容易添加一个自定义拦截器对请求和返回结果进行处理

二、Android Studio中如何集成?

在build.gradle中添加如下的依赖即可:

implementation("com.squareup.okhttp3:okhttp:4.9.0")

三、发送请求

1.Get请求

/**

* get 同步Get同求

*

* @param url url

* @return

*/

public String syncGet(String url) {

String result = null;

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()

.url(url)

.build();

try (Response response = client.newCall(request).execute()) {

result = response.body().string();

} catch (IOException e) {

e.printStackTrace();

}

return result;

}

/**

* get 异步Get同求

*

* @param url url

* @return

*/

public void nonSyncGet(String url, Callback responseCallback) {

String result = null;

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()

.url(url)

.build();

Call call = client.newCall(request);

call.enqueue(responseCallback);

}

2.Post请求

/**

* get 同步Post同求

*

* @param url url

* @return

*/

public String syncPost(String url) {

String result = null;

String json = "test";

OkHttpClient client = new OkHttpClient();

MediaType JSON = MediaType.parse("application/json; charset=utf-8");

RequestBody body = RequestBody.create(JSON, json);

Request request = new Request.Builder()

.url(url)

.post(body)

.build();

Response response = null;

try {

response = client.newCall(request).execute();

if (response.isSuccessful()) {

result = response.body().string();

} else {

throw new IOException("Unexpected code " + response);

}

} catch (IOException e) {

e.printStackTrace();

}

return result;

}

/**

* get 异步Post同求

*

* @param url url

* @return

*/

public void nonSyncPost(String url, Callback responseCallback) {

String json = "test";

OkHttpClient client = new OkHttpClient();

MediaType JSON = MediaType.parse("application/json; charset=utf-8");

RequestBody body = RequestBody.create(JSON, json);

Request request = new Request.Builder()

.url(url)

.post(body)

.build();

try {

client.newCall(request).enqueue(responseCallback);

} catch (Exception e) {

e.printStackTrace();

}

}

3.拦截器的简单使用

/**

* 拦截器的简单使用

*

* @param url url

* @return

*/

public void okInterceptor(String url, Callback responseCallback) {

String json = "test";

OkHttpClient client = new OkHttpClient();

//测试添加拦截器

client.newBuilder().addInterceptor(new TestInterceptor());

MediaType JSON = MediaType.parse("application/json; charset=utf-8");

RequestBody body = RequestBody.create(JSON, json);

Request request = new Request.Builder()

.url(url)

.post(body)

.build();

try {

client.newCall(request).enqueue(responseCallback);

} catch (Exception e) {

e.printStackTrace();

}

}

class TestInterceptor implements Interceptor {

@NotNull

@Override

public Response intercept(@NotNull Chain chain) throws IOException {

Request request = chain.request();

long t1 = System.nanoTime();

Log.i("ok", String.format("Sending request %s on %s%n%s",

request.url(), chain.connection(), request.headers()));

Response response = chain.proceed(request);

long t2 = System.nanoTime();

Log.i("ok", String.format("Received response for %s in %.1fms%n%s",

response.request().url(), (t2 - t1) / 1e6d, response.headers()));

return response;

}

}

4、OKHttp中有哪些有效的常量

b1cf0b574e74

image.png

另外有一篇文章把OKHttp的流程总结的不错,可以参考:神秘网友

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐