• okhttp添加公共参数


    项目开发中很多时候后台都会给一些全局的公共入参,比如携带手机信息或者时间戳等字段。而我们在使用okhttp时,就需要我们单独就行二次封装处理了,对于请求全局参数,每次请求都要去写一次,那是肯定不行的。
    所以就要我们自己处理。
    okhttp一个强大的功能拦截器,该功能采用责任链模式,不清楚的自行百度。我采用的就是这种方案。
    需要用到的知识点HttpUrl,FormBody
    直接用代码解说

    
    import android.util.Log
    import okhttp3.FormBody
    import okhttp3.HttpUrl
    import okhttp3.Interceptor
    import okhttp3.Request
    import okhttp3.Response
    
    private const val TAG = "RequestInterceptor"
    
    /**
     * 自定义的拦截器
     */
    class RequestInterceptor : Interceptor {
    
        override fun intercept(chain: Interceptor.Chain): Response {
            // 旧的请求体
            val oldRequest = chain.request()
            var newFormbodyData: FormBody? = null
            var newRequest: Request? = null
            if ("POST" == oldRequest.method) {
                // 公共参数,所有请求必须携带的,我们重新new一个请求体FormBody
                // 这个请求体只包含我们共有的请求参数
                val builder = FormBody.Builder()
                builder.addEncoded("brand", "Xiaomi")
                builder.addEncoded("model", "MIX+2")
                val formBody = builder.build()
                // 获取我们旧的请求体
                val requestBody = oldRequest.body
                // 创建我们最终的请求体
                val newFormBody = FormBody.Builder()
                // 将共有的请求参数放入我们最终的请求体中
                for (i in 0 until formBody.size) {
                    newFormBody.addEncoded(formBody.encodedName(i), formBody.encodedValue(i))
                }
    
                // 如果配合retrofit使用,需要注意一点,在发送的post请求如果没有自己独有的字段
                // 一定要对requestBody判断是不是formBody
                requestBody?.let {
                    // 通过判断之前旧的请求体是否包含有请求参数
                    // 如果有的话就添加进最终请求体
                    if (it.contentLength() > 0) {
                        val formBody1 = it as FormBody
                        for (i in 0 until formBody1.size) {
                            newFormBody.addEncoded(formBody1.encodedName(i), formBody1.encodedValue(i))
                        }
                    }
    
                }
                //  拿到我们最终的请求体
                newFormbodyData = newFormBody.build()
                // 重新构建一个requsest 将请求体放入进去
                newRequest = oldRequest.newBuilder().post(newFormbodyData!!).build()
    
            } else if ("GET" == oldRequest.method) {
                // 如果是get请求,我们创建一个新的HttpUrl
                // 用来储存我们的scheme,host,path
                // 这段代码相当于最原始的get请求配置的信息
                //   val httpurl = HttpUrl.Builder()
                //            .scheme("https")
                //            .host("www.hxeduonline.com")
                //            .addPathSegments("mobileapi2")
                //            .addPathSegments("index.php")
                //            .addQueryParameter("act", "xxx")
                //            .addQueryParameter("op", "xxxx")
    
                val publicParameter = HttpUrl.Builder()
                val httpUrl = oldRequest.url
                val pathSegments = httpUrl.pathSegments
                publicParameter.scheme(httpUrl.scheme)
                publicParameter.host(httpUrl.host)
                // 将path路径赋值给最新的HttpUrl
                pathSegments.forEach {
                    publicParameter.addEncodedPathSegments(it)
                    Log.d(TAG, "intercept: $it")
                }
                // 设置共有的参数
                publicParameter.addEncodedQueryParameter("brand", "Xiaomi")
                publicParameter.addEncodedQueryParameter("model", "MIX+2")
    
                // 将接口自带的参数放入到最终的httpUrl
                for (i in 0 until httpUrl.querySize) {
                    publicParameter.addQueryParameter(
                        httpUrl.queryParameterName(i),
                        httpUrl.queryParameterValue(i)
                    )
                }
                // 构建httpUrl
                val build = publicParameter.build()
                // 构建一个新的request
                newRequest = oldRequest.newBuilder().url(build).get().build()
    
            }
            // 全局配置参数就完成了,思路基本就是这样,扩展可以根据自己需求,比如加密都可以在这里处理
            return chain.proceed(newRequest!!)
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
  • 相关阅读:
    C++随心记(四)
    测试15k薪资第1步 —— 自动化测试理论基础
    C语言qsort()函数详细解析
    linux cpu物理信息查看
    MySQL主从配置的一些问题
    如何完成视频合并操作?这几个方法值得一试
    Element 中图片预览后如何快速关闭
    (九)学习笔记:动手深度学习(多层感知机 + 代码实现)
    Day 63 双向循环链表
    构建智慧银行:现代化系统架构的探索与实践
  • 原文地址:https://blog.csdn.net/u012108336/article/details/134307112