1、权限申请。网络权限,如果需要存取文件,也要申请存取权限,注意动态申请权限,
2、导包:
implementation("com.squareup.okhttp3:okhttp:4.9.1")
implementation("com.squareup.okhttp3:logging-interceptor")
3、get请求
3.1、同步请求
3.1.1、client对象
3.1.2、构造请求体:Request.Builder().url("url").build()
3.1.3、构造请求对象:client.newCall(request)
3.1.4、发起同步请求execute:response = call.execute()
3.1.5、获取返回值:response.body.toString()
注意:请求时耗时的,不能放在主线程中,需要运行在子线程中
3.2、异步请求
3.2.1、client对象
3.2.2、构造请求体:Request.Builder().url("url").build()
3.2.3、构造请求对象:client.newCall(request)
3.2.4、发起同步请求call.enqueue(
object :Callback{
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
//获取返回值:response.body.toString()
val body = response.body.toString()
Log.e("测试", "$body")
}
}
)
3.2.5、获取返回值:response.body.toString()
4、post请求
4.1、post同步请求
4.1.1、client对象
4.1.2、FormBody对象。如果带参,用FormBody添加参数。
4.1.3、构造请求体:Request.Builder().url("url").post(body).build()
4.1.4、构造请求对象:client.newCall(request)
4.1.5、发起同步请求execute:response = call.execute()
4.1.6、获取返回值:response.body.toString()
注意:需要在子线程中发起请求。
4.2、post异步请求
4.2.1、client对象
4.2.2、FormBody对象。如果带参,用FormBody添加参数。
4.2.3、构造请求体:Request.Builder().url("url").post(body).build()
4.2.4、构造请求对象:client.newCall(request)
4.2.5、发起同步请求enqueue:call.enqueue(
object :Callback{
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
//获取返回值:response.body.toString()
val body = response.body.toString()
Log.e("测试", "$body")
}
}
)
4.2.6、获取返回值:response.body.toString()
4.3、post异步请求(多表单文件上传)
4.3.1、client对象
4.3.2、MultipartyBody对象。如果带参,用MultipartyBody添加参数。MultipartyBody可以
添加文件:
addFormDataPart(
“file”,
"file具体名字",
RequestBody.create("application/octet-stream".toMediaType(),file对象)
)
4.3.3、构造请求体:Request.Builder().url("url").post(body).build()
4.3.4、构造请求对象:client.newCall(request)
4.3.5、发起同步请求enqueue:call.enqueue(
object :Callback{
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
//获取返回值:response.body.toString()
val body = response.body.toString()
Log.e("测试", "$body")
}
}
)
4.3.6、获取返回值:response.body.toString()
4.4、post异步请求(提交字符串)
4.4.1、client对象
4.4.2、JSONObject对象。使用put方法添加需要传递的键值对。
4.4.3、RequestBody对象。RequestBody.create(
/*如果是纯文本用:text/plain;charset=utf-8*/
"application/json;charset=utf-8".toMediaType(),
jsonObject.toString()
)
4.4.4、构造请求体:Request.Builder().url("url").post(body).build()
4.4.5、构造请求对象:client.newCall(request)
4.4.6、发起同步请求enqueue:call.enqueue(
object :Callback{
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
//获取返回值:response.body.toString()
val body = response.body.toString()
Log.e("测试", "$body")
}
}
)
4.4.7、获取返回值:response.body.toString()
请求的过程大同小异,另外还有put请求,put请求可以参考post请求。
5、拦截器LoggingInterceptor
5.1、OkHttp中也有拦截器,可以重写一个类,继承Interceptor类,重写里面的intercept()方法,定义自己需要拦截打印的信息,需要注意的是该方法返回的是一个Response类对象,而OkHttp中的response只能读取一次,再次使用会报错,所以在返回response时,需要重新定义一个携带所有信息的response,当然在这里就可以改变自己想返回的信息。
5.2、也可以直接使用HttpLoggingInterceptor,但是这样打印信息都是默认格式,不能自定义想打印的,使用方式如下:
/*init 方法最先运行*/ init { val httpLoggingInterceptor = HttpLoggingInterceptor() httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY) client = OkHttpClient.Builder() //builder构造者设计模式 .connectTimeout(10,TimeUnit.SECONDS) //连接超时时间 .readTimeout(10,TimeUnit.SECONDS) //读取超时 .writeTimeout(10,TimeUnit.SECONDS) //写超时,也就是请求超时 .addInterceptor(HttpInterceptor()) .build() }
6、Android8.0理论上不允许使用http,如果必须使用http就在application节点上加上:
android:usesCleartextTraffic="true"