大名鼎鼎的OkHttp
0.9、添加网络权限
<uses-permission android:name="android.permission.INTERNET" />
1、先导入依赖文件
implementation "com.squareup.okhttp3:okhttp:4.4.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.4.0" //日志拦截依赖 帮我们打印日志
2、封装请求网络方法
------单例模式
构造方法私有化

------get 请求方法 需Handler 切换主线程 回调数据
1 .构建请求

2 .创建Call对象

------POST 请求方法 需Handler 切换主线程 回调数据
FormBody 存放请求体中的键值对数据
RequestBody 请求体


------接口回调

网友评论