浅析okhttp3原理(Android)

大多数android开发者应该都比较熟悉okhttp这款首屈一指的网络请求框架,在使用上,相信大多数人都没压力。但是作为技术控,不能只停留在用的层面上,如果能深入研究下框架的实现,对于我们自身技术的提高也是很有帮助的。
废话不多说,首先看一张图:


浅析okhttp3原理(Android)_第1张图片
okhttp流程图.png

新手上路——使用okhttp

GET请求:

OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

POST请求:

OkHttpClient client = new OkHttpClient();
//创建表单请求体
FormBody.Builder formBody = new FormBody.Builder();
formBody.add("key", "value");
Request request = new Request.Builder()
        .url("https://www.google.com")
        .post(formBody.build())//传递请求体
        .build();
client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {

    }

    @Override
    public void onResponse(Call call, okhttp3.Response response) throws IOException {

    }
});

POST请求传递参数

post请求支持表单以及json文件作为入参。

先看一下Request.Builder类的post方法的声明:

public Builder post(RequestBody body)

由方法的声明可以看出,post方法接收的参数是RequestBody 对象,所以只要是RequestBody 类以及子类对象都可以当作参数进行传递,FormBody就是RequestBody 的一个子类。

1. 使用FormBody传递键值对参数

OkHttpClient client = new OkHttpClient();
//创建表单请求体
FormBody.Builder formBody = new FormBody.Builder();
formBody.add("key", "value");
Request request = new Request.Builder()
        .url("https://www.google.com")
        .post(formBody.build())//传递请求体
        .build();
client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {

    }

    @Override
    public void onResponse(Call call, okhttp3.Response response) throws IOException {

    }
});

2. 使用RequestBody传递Json或File对象

上传json对象使用示例如下:
OkHttpClient client = new OkHttpClient();
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
String jsonStr = "{\"id\":\"123456\",\"name\":\"json\"}";
RequestBody body = RequestBody.create(JSON, jsonStr);
Request request = new Request.Builder()
        .url("https://www.google.com")
        .post(body)
        .build();
client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {

    }

    @Override
    public void onResponse(Call call, okhttp3.Response response) throws IOException {

    }
});

上传File对象使用示例如下:
OkHttpClient client = new OkHttpClient();
MediaType fileType = MediaType.parse("File/*");
File file = new File("path");//file对象.
RequestBody body = RequestBody.create(fileType , file );
Request request = new Request.Builder()
        .url("https://www.google.com")
        .post(body)
        .build();
client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {

    }

    @Override
    public void onResponse(Call call, okhttp3.Response response) throws IOException {

    }
});

3.使用MultipartBody同时传递键值对参数和File对象

File file = new File("输入你的文件路径");
OkHttpClient client = new OkHttpClient();
MultipartBody multipartBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("id", "123456")
        .addFormDataPart("name", "multipartbody")
        .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("file/*"), file))
        .build();
Request request = new Request.Builder()
        .url("url")
        .post(multipartBody)
        .build();
client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {

    }

    @Override
    public void onResponse(Call call, okhttp3.Response response) throws IOException {

    }
});

OkHttp源码分析

--------待续---------------

HTTP2.0关于多路复用的研究

OkHttp官方Github地址

PS:该文章仅供个人学习之用,学识浅薄,不当之处,还请指出!

你可能感兴趣的:(浅析okhttp3原理(Android))