Kotlin OKHTTP3和拦截器的使用

本文介绍了在Android6.0及更高版本中进行网络请求时,如何配置`android:usesCleartextTraffic`属性,并使用OkHttp3进行同步和异步GET、POST请求,包括表单、文件上传以及自定义OkHttp3拦截器以记录请求和响应信息。
摘要由CSDN通过智能技术生成

注意:在android6.0以后网络请求还需如下配置:

 android:usesCleartextTraffic="true"

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:usesCleartextTraffic="true"
    android:theme="@style/AppTheme">
  ***
</application>

一、引入okhttp3包

 //okhttp3
 implementation("com.squareup.okhttp3:okhttp:4.9.0")
 implementation("com.squareup.okhttp3:logging-interceptor:4.9.0")

二、创建单例对象

package com.example.buju.http

import android.content.Context
import android.os.Environment
import android.util.Log
import android.widget.Toast
import okhttp3.Call
import okhttp3.Callback
import okhttp3.FormBody
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import org.json.JSONObject
import java.io.File
import java.io.IOException
import java.util.concurrent.TimeUnit
/**
 * 单例对象
 *  -http请求工具类
 * */
object HiOkhttp {

    /*  private val client:OkHttpClient
        // 最早调用模块
        init {
             val httpLoggingInterceptor = HttpLoggingInterceptor()// okhttp3自带拦截器
             httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
             client = OkHttpClient.Builder()// builder构造者设计模式
                 .connectTimeout(10,TimeUnit.SECONDS)//连接超时时间
                 .readTimeout(10,TimeUnit.SECONDS)// 读取超时
                 .writeTimeout(10,TimeUnit.SECONDS)// 写超时
                 .addInterceptor(httpLoggingInterceptor)// 自带拦截器
                 .build()

         }*/

    val client = OkHttpClient.Builder()// builder构造者设计模式
        .connectTimeout(10,TimeUnit.SECONDS)//连接超时时间
        .readTimeout(10,TimeUnit.SECONDS)// 读取超时
        .writeTimeout(10,TimeUnit.SECONDS)// 写超时
        .addInterceptor(LoggingInterceptor())// 自定义拦截器
        .build()

    // android 分为主线程和子线程
    // 主线程就是APP一启动后,咱们android framework层会启动一个线程,主线程(UI线程)
    // 子线程 - new Thread().start()


    /**
     * get请求
     *  -同步请求不要放在主线程中执行,容易阻塞线程
     * */
    fun syncGet(url:String){
        // 构造请求体
        val request:Request = Request.Builder()
            .url(url)
            .build()

        // 构造请求对象
        val call:Call = client.newCall(request)

      Thread(Runnable {// 构建子线程
          // execute() - 发起同步请求(同步执行)
          val  response:Response = call.execute()
          val body: String? = response.body?.string()
          Log.e("OKHTTP","get response:${body}")
      }).start()
    }

    /**
     * get请求
     *  -异步请求
     * */
    fun asyncGet(url:String){
        // 构造请求体
        val request:Request = Request.Builder()
            .url(url)
            .build()

        // 构造请求对象
        val call:Call = client.newCall(request)

        // enqueue() - 发起异步请求(异步执行)
       call.enqueue(object :Callback{
            override fun onFailure(call: Call, e: IOException) {
                Log.e("OKHTTP","get onFailure:${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                val body:String? = response.body?.string()
                Log.e("OKHTTP","get response:${body}")
            }

        })

    }

    /**
     * post请求
     *  -同步表单请求,不要放在主线程中执行,容易阻塞线程
     * */
    fun syncPost(url:String){
        val body = FormBody.Builder()
            .add("userId","123")
            .add("userName","zs")
            .build()

        val request:Request = Request.Builder()
            .url(url)
            .post(body)
            .build()

        val call:Call = client.newCall(request)

        Thread(Runnable {
            val response:Response = call.execute()
            val resultBody = response.body?.string()
            Log.e("OKHTTP","post response:${resultBody}")
        }).start()

    }

    /**
     * post请求
     *  -异步表单请求
     * */
    fun asyncPost(url:String){
        val body = FormBody.Builder()
            .add("userId","123")
            .add("userName","zs")
            .build()

        val request:Request = Request.Builder()
            .url(url)
            .post(body)
            .build()

        val call:Call = client.newCall(request)

        call.enqueue(object :Callback{
            override fun onFailure(call: Call, e: IOException) {
                Log.e("OKHTTP","post onFailure:${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                val resultBody = response.body?.string()
                Log.e("OKHTTP","post response:${resultBody}")
            }

        })


    }


    /**
     * post请求
     *  -异步多表单文件上传请求
     *  -url必须支持文件上传的接口
     *  -android6.0以后,读取外部存储卡的文件都是需要动态申请权限
     * */
    fun asyncPostMultipart(url:String,context:Context){
        val file = File(Environment.getExternalStorageDirectory(),"demo.png")
        if (!file.exists()){
            Toast.makeText(context,"文件不存在",Toast.LENGTH_LONG).show()
            return
        }

        val body = MultipartBody.Builder()
            .addFormDataPart("key1","value1")
            .addFormDataPart("key2","value2")
            .addFormDataPart("file",
                "file.png",
                RequestBody.create("application/octet-stream".toMediaType(), file))
            .build()

        val request:Request = Request.Builder()
            .url(url)
            .post(body)
            .build()

        val call:Call = client.newCall(request)

        call.enqueue(object :Callback{
            override fun onFailure(call: Call, e: IOException) {
                Log.e("OKHTTP","post onFailure:${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                val resultBody = response.body?.string()
                Log.e("OKHTTP","post response:${resultBody}")
            }

        })


    }

    /**
     * post请求
     *  -异步字符串请求
     * */
    fun asyncPostString(url:String){
        // 纯文本
       /* val textPlain = "text/plain;charset=utf-8".toMediaType()

        val textObj = "username:username;password:1234"

        val body = RequestBody.create(textPlain,textObj)*/
        // json对象
        val applicationJson = "application/json;charset=utf-8".toMediaType();

        val jsonObj = JSONObject()
        jsonObj.put("key1","val1")
        jsonObj.put("key2","val2")

        val body = RequestBody.create(applicationJson,jsonObj.toString())

        val request = Request.Builder()
            .url(url)
            .post(body)
            .build()

        val call:Call = client.newCall(request)

        call.enqueue(object :Callback{
            override fun onFailure(call: Call, e: IOException) {
                Log.e("OKHTTP","post onFailure:${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                val resultBody = response.body?.string()
                Log.e("OKHTTP","post response:${resultBody}")
            }

        })



    }


}

三、自定义okhttp3拦截器

package com.example.buju.http

import android.util.Log
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody
import okio.Buffer

/**
 * OKHttp3 拦截器
 * */
class LoggingInterceptor:Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val time_start = System.nanoTime()// 开始时间戳
        val request = chain.request()// 请求
        val response = chain.proceed(request)// 响应

        /**
         * 获取请求信息
         * */
        val buffer = Buffer()// okio.Buffer
        request.body?.writeTo(buffer)
        val requestBodyStr = buffer.readUtf8()
        // request.url - 请求接口;requestBodyStr - 请求携带的参数
        Log.e("OKHTTP",String.format("Sending request %s with params %s",request.url,requestBodyStr))

       /**
        * 获取响应信息
        * */
        val bussinessData:String = response.body?.string()?:"response body null"
        val mediaType = response.body?.contentType()
        val newBody = ResponseBody.create(mediaType,bussinessData)
        val newResponse = response.newBuilder().body(newBody).build()

        val time_end = System.nanoTime()//结束始时间戳
        //  request.url - 请求接口;(time_end - time_start) -执行时间;bussinessData - 响应数据
        Log.e("OKHTTP",String.format("Received response for %s in %.1fms >>> %s",request.url,(time_end - time_start),bussinessData))

        return newResponse







    }

}

Android Kotlin使用 OkHttp3 下载文件并带有下载进度,可以通过以下步骤实现: 1. 添加 OkHttp3 依赖 在 app module 的 build.gradle 文件中添加以下代码: ``` dependencies { implementation 'com.squareup.okhttp3:okhttp:4.9.0' } ``` 2. 创建 OkHttp3 客户端 在代码中创建一个 OkHttpClient 客户端: ``` val client = OkHttpClient() ``` 3. 创建下载请求 使用 OkHttp3 的 Request.Builder 创建一个下载请求,并设置下载 URL 和保存文件的路径: ``` val request = Request.Builder() .url("https://example.com/file.zip") .build() ``` 4. 创建下载监听器 定义一个回调接口,用于监听下载进度: ``` interface DownloadListener { fun onDownloadProgress(progress: Int) } ``` 在代码实现这个接口,并在其中更新下载进度,例如: ``` val listener = object : DownloadListener { override fun onDownloadProgress(progress: Int) { runOnUiThread { // 更新下载进度条 progressBar.progress = progress } } } ``` 5. 发起下载请求 使用 OkHttpClient 的 newCall 方法发起下载请求,并在 enqueue 方法中传入一个 Callback 参数,该参数将在下载完成时回调: ``` client.newCall(request).enqueue(object : Callback { override fun onFailure(call: Call, e: IOException) { // 下载失败 } override fun onResponse(call: Call, response: Response) { val inputStream: InputStream = response.body?.byteStream() ?: return // 保存文件并更新下载进度 val totalSize: Long = response.body?.contentLength() ?: -1 var downloadedSize: Long = 0 val outputStream = FileOutputStream("/storage/emulated/0/Download/file.zip") val buffer = ByteArray(8192) while (true) { val bytes = inputStream.read(buffer) if (bytes == -1) break outputStream.write(buffer, 0, bytes) downloadedSize += bytes val progress = (downloadedSize * 100 / totalSize).toInt() listener.onDownloadProgress(progress) } outputStream.close() inputStream.close() // 下载完成 } }) ``` 这段代码中,我们首先从 response.body 中获取输入流并创建输出流,然后使用循环逐段读取输入流的数据,再将其写入输出流,并计算下载进度,最后调用 DownloadListener 的 onDownloadProgress 方法更新下载进度。在下载完成后,我们需要关闭输入流和输出流,以及在 onFailure 方法中处理下载失败的情况。 6. 完整代码 最终的代码应该类似于这样: ``` interface DownloadListener { fun onDownloadProgress(progress: Int) } val client = OkHttpClient() val request = Request.Builder() .url("https://example.com/file.zip") .build() val listener = object : DownloadListener { override fun onDownloadProgress(progress: Int) { runOnUiThread { // 更新下载进度条 progressBar.progress = progress } } } client.newCall(request).enqueue(object : Callback { override fun onFailure(call: Call, e: IOException) { // 下载失败 } override fun onResponse(call: Call, response: Response) { val inputStream: InputStream = response.body?.byteStream() ?: return // 保存文件并更新下载进度 val totalSize: Long = response.body?.contentLength() ?: -1 var downloadedSize: Long = 0 val outputStream = FileOutputStream("/storage/emulated/0/Download/file.zip") val buffer = ByteArray(8192) while (true) { val bytes = inputStream.read(buffer) if (bytes == -1) break outputStream.write(buffer, 0, bytes) downloadedSize += bytes val progress = (downloadedSize * 100 / totalSize).toInt() listener.onDownloadProgress(progress) } outputStream.close() inputStream.close() // 下载完成 } }) ``` 注意,这段代码中保存文件的路径是硬编码的,你需要根据实际需求修改它。另外,为了更新 UI,我们需要在 onDownloadProgress 方法中使用 runOnUiThread 方法,以确保在主线程中执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值