OkHttp3简介与使用

OkHttp是一个非常高效的Http客户端,近年来几乎所有的Android应用都会使用它作为网络访问的框架。它有以下几个重要的特点:

  • 支持HTTP/2;

  • 支持连接复用;

  • 内部维护连接池;

  • 支持GZIP压缩,节省流量;

  • 维护缓存,提高响应效率;

  • 支持自动重连;

  • 等等

异步GET请求

String url = "http://wwww.baidu.com";
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder()
        .url(url)
        .get()//默认就是GET请求,可以不写
        .build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {//加入到请求队列中,不会阻塞
    @Override
    public void onFailure(Call call, IOException e) {
        Log.d(TAG, "onFailure: ");
    }
    @Override
    public void onResponse(Call call, Response response) throws IOException {
        Log.d(TAG, "onResponse: " + response.body().string());
    }
});

同步GET请求

String url = "http://wwww.baidu.com";
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder()
        .url(url)
        .get()
        .build();
final Call call = okHttpClient.newCall(request);
Response response = call.execute();//在这里会阻塞住,直到请求响应返回

POST方式提交字符串

//请求/响应的body部分的格式为json格式
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)//post发送
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

这种方式与前面的区别就是在构造Request对象时,需要多构造一个RequestBody对象,用它来携带我们要提交的数据。在构造 RequestBody 需要指定MediaType,用于描述请求/响应 body 的内容类型。

RequstBody的几种构造方式:
在这里插入图片描述
故可以看出RequstBody的形式有:文件,字节数组,字符串,ByteString,以及数据块类型;

post方式提交流

RequestBody requestBody = new RequestBody() {
    @Nullable
    @Override
    public MediaType contentType() {
        return MediaType.parse("text/x-markdown; charset=utf-8");
    }@Override
    public void writeTo(BufferedSink sink) throws IOException {
        sink.writeUtf8("post commit sink");
    }
};
​
Request request = new Request.Builder()
        .url("https://api.github.com/markdown/raw")
        .post(requestBody)
        .build();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        Log.d(TAG, "onFailure: " + e.getMessage());
    }@Override
    public void onResponse(Call call, Response response) throws IOException {
    }
});

这个例子是流直接写入Okio的BufferedSink。有可能会使用OutputStream,可以使用BufferedSink.outputStream()来获取。

POST提交文件

public static final MediaType MEDIA_TYPE_MARKDOWN
  = MediaType.parse("text/x-markdown; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
    File file = new File("README.md");//本地文件
    Request request = new Request.Builder()
        .url("https://api.github.com/markdown/raw")
        .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
        .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.d(TAG, "onFailure: " + e.getMessage());
        }@Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.d(TAG, "onResponse: " + response.body().string());
        }
});
}

POST方式提交表单

OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
        .add("search", "Jurassic Park")
        .build();
Request request = new Request.Builder()
        .url("https://en.wikipedia.org/w/index.php")
        .post(requestBody)
        .build();
​
okHttpClient.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        Log.d(TAG, "onFailure: " + e.getMessage());
    }@Override
    public void onResponse(Call call, Response response) throws IOException {
        Log.d(TAG, "onResponse: " + response.body().string());
    }
});

POST方式提交分块请求

private static final String IMGUR_CLIENT_ID = "...";
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");private void postMultipartBody() {
    OkHttpClient client = new OkHttpClient();
    // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
    MultipartBody body = new MultipartBody.Builder("AaB03x")
            .setType(MultipartBody.FORM)
            .addPart(
                    Headers.of("Content-Disposition", "form-data; name=\"title\""),
                    RequestBody.create(null, "Square Logo"))
            .addPart(
                    Headers.of("Content-Disposition", "form-data; name=\"image\""),
                    RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
            .build();
​
    Request request = new Request.Builder()
            .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
            .url("https://api.imgur.com/3/image")
            .post(body)
            .build();
​
    Call call = client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
        }@Override
        public void onResponse(Call call, Response response) throws IOException {
            System.out.println(response.body().string());}});
}

MultipartBuilder可以构建复杂的请求体,与HTML文件上传形式兼容。多块请求体中每块请求都是一个请求体,可以定义自己的请求头。这些请求头可以用来描述这块请求,例如他的Content-Disposition。如果Content-Length和Content-Type可用的话,他们会被自动添加到请求头中。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于OkHttp3的封装使用,你可以按照以下步骤进行: 1. 添加OkHttp3依赖:在你的项目中的build.gradle文件中添加以下依赖: ```groovy dependencies { implementation 'com.squareup.okhttp3:okhttp:4.9.1' } ``` 2. 创建OkHttpClient实例:在你的代码中创建一个OkHttpClient实例,可以设置一些参数,如连接超时时间、读写超时时间等。 ```java OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) // 设置连接超时时间为10秒 .readTimeout(10, TimeUnit.SECONDS) // 设置读取超时时间为10秒 .writeTimeout(10, TimeUnit.SECONDS) // 设置写入超时时间为10秒 .build(); ``` 3. 创建Request对象:根据你的请求需求,创建一个Request对象,包括请求的URL、请求方法(GET、POST等)、请求头、请求体等。 ```java Request request = new Request.Builder() .url("http://www.example.com/api") // 设置请求的URL .addHeader("Authorization", "Bearer token") // 设置请求头,如添加认证信息 .post(RequestBody.create(MediaType.parse("application/json"), requestBody)) // 设置请求体,如发送JSON数据 .build(); ``` 4. 发送请求并处理响应:使用OkHttpClient实例发送请求,并处理返回的响应。 ```java try { Response response = client.newCall(request).execute(); if (response.isSuccessful()) { String responseBody = response.body().string(); // 处理响应结果 } else { // 处理请求失败 } } catch (IOException e) { e.printStackTrace(); // 处理异常 } ``` 这是一个基本的OkHttp3的封装使用示例,你可以根据自己的需求进行进一步定制和扩展。希望对你有帮助!如果有其他问题,可以继续问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值