一、GET同步请求

object HiHttp{
    private const val BASE_URL="服务器地址"
    val client= OkHttpClient.Builder()
        //超时设置
        .connectTimeout(10, TimeUnit.SECONDS)
        .readTimeout(10, TimeUnit.SECONDS)
        .writeTimeout(10, TimeUnit.SECONDS)
        .build()
    fun get(){
        //GET同步只能在子线程中运行
        Thread(Runnable{
            val request = Request.Builder()
                .url(BASE_URL+"接口")
                .build()
            val call =client.newCall(request)
            val response=call.execute()
            val body=response.body?.string()
            println("GET同步: ${body}")
        }).start()
    }
}

二、GET异步

//异步请求可以在主线程中运行

fun getAsync(){
    val request = Request.Builder()
        .url(BASE_URL+接口")
        .build()
    val call =client.newCall(request)
    call.enqueue(object: Callback{
        override fun onFailure(call: Call, e: IOException) {
            Log.e("TAG", "onFailure: ${e.message}", )
        }

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

    })
}

三、POST同步

fun post(){
    val body= FormBody.Builder()
        //例如userID “123456”
        .add("key","value")
        .build()
    val request= Request.Builder()
        .url(BASE_URL+"/接口")
        .post(body)
        .build()
    val call = client.newCall(request)
    Thread(Runnable{
        val response=call.execute()
        Log.e("TAG", "post: ${response.body?.string()}", )
    }).start()
}

四、POST异步

fun postAsync(){
    val body= FormBody.Builder()
        .add("key","value")
        .build()
    val request= Request.Builder()
        .url(BASE_URL+"/upload")
        .post(body)
        .build()
    val call = client.newCall(request)

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

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

    })

五、异步表单文件上传

fun postAsyncMultipart(filePath: String){
    val file = File(filePath)
    if(!file.exists()||!file.isFile){
        Log.e("TAG", "postAsyncMultipart: 文件不存在或不是文件")
        return
    }
//这里指定上传的是MP4
    val videoMediaType="video/mp4".toMediaType()
    val requestBody= MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("file",file.name,file.asRequestBody())
        .build()
    val request= Request.Builder()
        .url(BASE_URL+"接口")
        .post(requestBody)
        .build()
    val call=client.newCall(request)
    call.enqueue(object : Callback{
        override fun onFailure(call: Call, e: IOException) {
            Log.e("TAG", "onFailure: ${e.message}", )
        }

        override fun onResponse(call: Call, response: Response) {
            Log.e("TAG", "onResponse: ${response.body?.string()}", )
        }

    })
}

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐