Learn a common framework in Android every day -- 3. Okhttp

Relieving Sichuan 2020-11-13 03:45:59
learn common framework android day


1. brief introduction

in the past ,Android Send on HTTP There are generally two ways to request :HttpURLConnection and HttpClient(Apache HttpClient). But due to the HttpClient There is API Too many 、 It's difficult to expand ,Android The team is increasingly not suggesting that we use this approach . Finally in the Android 6.0 In the system ,HttpClient Has been completely removed , It indicates that this function has been officially abandoned . Although the official recommendation is HttpURLConnection, But in the process of code writing, it will still be more cumbersome . here , A network layer framework to replace them has emerged : That is to say Okhttp, The framework is also where I am 《Android First line of code 》 What you learn is nothing more than Litepal The second frame after that . Today, I have learned again Okhttp, Record the learning process on a blog , I hope it will help the readers .
OkHttp It's by the great name Square company-developed , This company has contributed a lot to the cause of open source , except OkHttp outside , And developed something like Picasso,Retrofit And other famous open source projects .OkHttp Not only is it easy to do on interface encapsulation , Even in the underlying implementation, it's a school of its own , Compared with the original HttpURLConnection, It can be said that there are too many , Now it has become a vast Android Developer's preferred network communication library .

2. characteristic

OkHttp It's an efficient HTTP client , It has the following default features :

  • Support HTTP/2, Allow all requests with the same host address to share the same socket Connect
  • Connection pooling reduces request latency
  • Transparent GZIP Compression reduces the size of response data
  • Cache response content , Avoid completely repeated requests

When there's a problem with the Internet OkHttp Still stick to your duty , It will automatically recover general connection problems , If you have more than one service IP Address , When the first one IP When the request fails ,OkHttp Will alternate between trying your configuration of other IP,OkHttp Use modern TLS technology (SNI, ALPN) Initializing a new connection , When the handshake fails, it will go back to TLS 1.0.

OkHttp Support Android 2.3 And above Android platform , as well as Java,JDK 1.7 And above .

OkHttp The use of is very simple . Its request / Respond to API Use Constructor mode builders To design , It supports blocking Synchronization request And with callback Asynchronous requests .
In order to better demonstrate OkHttp, Only synchronization is shown here / Asynchronous execution get/post The relevance of the request api, As for OkHttp Related calls to , Suggestions for reference OkHttp Official documents of :OkHttp Official website , No need to turn over the wall
About use post Request to send flow / file / Forms etc. , These functions need to be combined with another framework :OkIo, It will be shown in the next blog .
Don't talk much , Let's start using OkHttp Well .

3. demonstration

3.1 Integrate

It's the integration link that we love , We just need to modify it module Under the build.gradle, Join in OkHttp Dependence , The code is as follows :

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

Remember again Sync once , Make sure OkHttp Integrated into your project . When the author wrote this blog OkHttp3 Stable version number to achieve 4.6.0, If the reader is in OkHttp An updated version of , Remember to update in time , Keep pace with the times .

3.2 To configure

Now that you've integrated OkHttp, It is necessary to carry out network related operations . In that case , You need to declare the network permissions under the manifest file , namely :

<uses-permission android:name="android.permission.INTERNET" />

in addition : If your Android Version is Android P( namely targetSdkVersion 27), Errors about the network may be reported when running the project , As shown in the figure :
 Insert picture description here
reason : stay Android P On the equipment of the system , If the application uses unencrypted plaintext traffic http Network request , This will cause the application to be unable to make network requests ,https Will not be affected , In the same way, if the application uses WebView Load web page Then loading the web page also needs to be https request .
resolvent

  • APP The overall network request is changed to https
  • take targetSdkVersion Version down to 27 following
  • Change project network security configuration

Three methods are also available , Here is the third solution , To broaden the solution

  1. stay res New under the directory xml Folder stay xml The new name in the folder is network_config( The name is not fixed ) Of xml, The code is as follows :
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
  1. then , stay AndroidManifest add android:networkSecurityConfig attribute , The code is as follows :
 <application
...
android:networkSecurityConfig="@xml/network_security_config"
...
/>
  1. The above two steps complete the network security configuration , If you can't, you can try another two methods or Baidu

3.3 Layout files and URL encapsulation

Next , Let's go straight to layout the document activity_main.xml Compiling . The layout is simple , There are only four buttons , The code is as follows :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn_get_syn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" send out get request —— Sync "/>
<Button
android:id="@+id/btn_get_asy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" send out get request —— asynchronous "/>
<Button
android:id="@+id/btn_post_syn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" send out post request —— asynchronous "/>
<Button
android:id="@+id/btn_post_asy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" send out post request —— asynchronous "/>
</LinearLayout>

after , Let's simply encapsulate the requested URL, namely http://wwww.baidu.com. It is suggested that you want to go deep OkHttp Of the readers themselves build a Tomcat/Apache The server , Upload the resource file to the server , Then visit the local path , This can deepen the understanding of network requests . Here in order to facilitate the demonstration of the direct network request Baidu , The code is as follows :

private static final String URL = "http://wwww.baidu.com";

3.4 Sync GET request

Sync GET The steps of the request are simple , It's mostly divided into :

  • structure OkHttpClient object ;
  • structure Request object ;
  • Build... From the objects in the first two steps Call object ;
  • adopt call.execute() Method to submit a synchronization request . Be careful , Because it's a synchronization request , So the api To put it in a child thread , Avoid blocking the main thread , cause ANR abnormal . in addition ,Android3.0 In the future, it is not allowed to access the network in the main thread .

The code is as follows :

private void getBySynchronized() {

btn_get_syn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

// 1. structure Client object 
OkHttpClient client = new OkHttpClient();
// 2. Build with builder mode and chain call Request object 
final Request request = new Request.Builder()
.url(URL) // request URL
.get() // The default is get request , Don't write 
.build();
// 3. adopt 1 and 2 Produced Client and Request Object to generate Call object 
final Call call = client.newCall(request);
// 4. The synchronous get Request needs to use execute() Method , And in order to prevent the main thread from blocking, it needs to be placed in the sub thread itself 
new Thread(new Runnable() {

@Override
public void run() {

try {

// 6. structure response object 
Response response = call.execute();
Log.d(TAG, " The synchronous get The request is successful ! The requested information is :" + response.body().string());
} catch (IOException e) {

e.printStackTrace();
}
}
}).start();
}
});
}

 Insert picture description here

3.5 asynchronous GET request

asynchronous GET The first few steps of the request are the same as synchronization , Just the last one is through call.enqueue(Callback()) To submit a request , The code is as follows :

private void getByAsynchronized() {

btn_get_asy.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

// 1. structure Client object 
OkHttpClient client = new OkHttpClient();
// 2. Build with builder mode and chain call Request object 
final Request request = new Request.Builder()
.url(URL) // request URL
.get() // The default is get request , Don't write 
.build();
// 3. adopt 1 and 2 Produced Client and Request Object to generate Call object 
Call call = client.newCall(request);
// 4. call Call Object's enqueue() Method , And implement a callback implementation class 
call.enqueue(new Callback() {

@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {

Log.d(TAG, " Send asynchronously get request was aborted !");
e.printStackTrace();
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

Log.d(TAG, " Send asynchronously get The request is successful ! The requested information is :" + response.body().string());
}
});
}
});
}

 Insert picture description here

3.6 Sync POST request

Sync POST The requested steps are similar to synchronization GET request , It's mostly divided into :

  • structure OkHttpClient object ;
  • structure FormBody object ( Key value pair )
  • structure Request object ;
  • Build... From the objects in the first two steps Call object ;
  • adopt call.execute() Method to submit a synchronization request .

The code is as follows :

private void postBySynchronized() {

btn_post_syn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

// 1. structure Client object 
OkHttpClient client = new OkHttpClient();
// 2. Use builder mode and chain call to build key value pair object 
FormBody formBody = new FormBody.Builder()
.add("username", "admin")
.add("password", "123456")
.build();
// 3. Build with builder mode and chain call Request object 
final Request request = new Request.Builder()
.url(URL) // request URL
.post(formBody) // The default is get request , Don't write 
.build();
// 4. adopt 1 and 3 Produced Client and Request Object to generate Call object 
final Call call = client.newCall(request);
// 5. The synchronous post Request needs to use execute() Method , And in order to prevent the main thread from blocking, it needs to be placed in the sub thread itself 
new Thread(new Runnable() {

@Override
public void run() {

try {

// 6. structure response object 
Response response = call.execute();
Log.d(TAG, " The synchronous post The request is successful ! The requested information is :" + response.body().string());
} catch (IOException e) {

e.printStackTrace();
}
}
}).start();
}
});
}

 Insert picture description here

3.7 asynchronous POST request

asynchronous POST The first few steps of the request are the same as synchronization , Just the last one is through call.enqueue(Callback()) To submit a request , The code is as follows :

private void postByAsynchronized() {

btn_post_asy.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

// 1. structure Client object 
OkHttpClient client = new OkHttpClient();
// 2. Use builder mode and chain call to build key value pair object 
FormBody formBody = new FormBody.Builder()
.add("username", "admin")
.add("password", "123456")
.build();
// 3. Build with builder mode and chain call Request object 
final Request request = new Request.Builder()
.url(URL) // request URL
.post(formBody) // The default is get request , Don't write 
.build();
// 4. adopt 1 and 3 Produced Client and Request Object to generate Call object 
Call call = client.newCall(request);
// 5. call Call Object's enqueue() Method , And implement a callback implementation class 
call.enqueue(new Callback() {

@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {

Log.d(TAG, " Send asynchronously post request was aborted !");
e.printStackTrace();
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

Log.d(TAG, " Send asynchronously post The request is successful ! The requested information is :" + response.body().string());
}
});
}
});
}

 Insert picture description here

4 summary

  • A synchronous invocation : Write simple , But it will block the main thread , Generally not applicable
  • Asynchronous call : The callback function is in the child thread , We can't update... In a child thread UI, It needs help from runOnUiThread() Methods or Handler To deal with it

Generally speaking , Although synchronization / Asynchronous calls have their own advantages and disadvantages , In the actual project , We are still Asynchronous call It's going to be used a little bit more , collocation RxJava/RxAndroid as well as EventBus, Can enhance the user experience . Of course , If it's for rapid development, it's simpler demo, A synchronous invocation That's enough. .

5. Source code address

AFL——Android Frame learning

版权声明
本文为[Relieving Sichuan]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple