Android network request framework, now a lot of things, such as the introduction of “Android Volley framework source analysis” and “Android Retrofit use”, in addition to OkHttp is Android network request framework is very good. And it is said that Android4.4 source can see HttpURLConnection has been replaced by OkHttp implementation, such an excellent network request framework, we need to learn about it.

There are plenty of articles on the web about how to use OkHttp, so why am I writing this article? One is to introduce OkHttp as a great framework for developers who have not yet used OkHttp, and the other is to collect the best OkHttp articles so that readers can find the best articles about learning OkHttp directly in this article.

Introduction to the

OkHttp is a valid HTTP client by default, and has the following advantages:

Connection pooling reduces request latency (if HTTP / 2 is not available);

2. Transparent GZIP reduces download size;

3. Response caching avoids a network of completely repeated requests.

Network request usage

1) Configure the environment

Add the OkHttp dependency to your app’s build.gradle

The compile 'com. Squareup. Okhttp3: okhttp: 3.9.1'Copy the code

Because OkHttp relies on OKio internally, we need to add the okio dependency as well:

The compile ‘com. Squareup. Okio: okio: 1.6.0’

Configure the environment can be used.

2) Get request
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url("https://interface.meiriyiwen.com/article/today?dev=1").build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i("test"."text------>"+response.body().string()); }});Copy the code

Note: Asynchronous requests are made using the call.enqueue method, whose onResponse callback is running in a thread.

3) POST requests to submit Json data
try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("version"."3.5");
            jsonObject.put("platform"."android");
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            RequestBody  body = RequestBody.create(JSON,jsonObject.toString());
            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request
                    .Builder()
                    .url("http://v3.wufazhuce.com:8000/api/hp/detail/1557")
                    .post(body)
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Log.i("test"."text------>"+response.body()); }}); } catch (JSONException e) { e.printStackTrace(); }Copy the code

Parse (“application/json; Charset = UTF-8 “) Specifies the JSON type. Create a RequestBody object and pass it in the Builder post method.

4) POST the form
OkHttpClient okHttpClient = new OkHttpClient();
        FormBody body = new FormBody
                .Builder()
                .add("version"."3.5")
                .add("platform"."android")
                .build();
        Request request = new Request
                .Builder()
                .url("http://v3.wufazhuce.com:8000/api/hp/detail/1557")
                .post(body)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i("test"."text------>"+response.body().string()); }});Copy the code

Note: We need to use the FormBody object, which is passed in as the key pair value of the form. Before OkHttp3.0, we used the FormEncodingBuilder object. After 3.0, FormEncodingBuilder has been replaced by FormBody.

This article also references some of the best articles on OkHttp. If you want to learn more about OkHttp, please refer to this article:

Android OkHttp Fully parsed It’s time to learn about OkHttp

OkHttp Tutorial

For the encapsulation of OkHttp, please refer to this article by Hongyang Daishen:

An Improved okHttp wrapper library for Android