kotlin中OKhttp的使用OkHttpClient()

kotlin中OKhttp的使用OkHttpClient()

fun getDataByGet(): Int {
        try {
            val client = OkHttpClient()
            val request = Request.Builder().get()
                .url("你的url")
                .build()

            val response = client.newCall(request)
            var call = client.newCall(request)
            //异步请求
            call.enqueue(object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                    Log.d("UPDATE", "onFailure: $e")
                }

                @Throws(IOException::class)
                override fun onResponse(call: Call, response: Response) {
                    Log.d("UPDATE", "OnResponse: " + response.body()?.string())
                }
            })
        }catch (e:Exception) {
            Log.e("UPDATE ERROR:", "", e)
        }
         return 1;
    }
发布了6 篇原创文章 · 获赞 1 · 访问量 1974

猜你喜欢

转载自blog.csdn.net/weixin_45444405/article/details/101515941