How does OkHttp get Json string?

后端 未结 5 1876
不知归路
不知归路 2020-12-02 08:50

Solution: It was a mistake on my side.

The right way is response.body().string() other than response.body.toString

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 09:34

    Below code is for getting data from online server using GET method and okHTTP library for android kotlin...

    Log.e("Main",response.body!!.string())

    in above line !! is the thing using which you can get the json from response body

    val client = OkHttpClient()
                val request: Request = Request.Builder()
                    .get()
                    .url("http://172.16.10.126:8789/test/path/jsonpage")
                    .addHeader("", "")
                    .addHeader("", "")
                    .build()
                client.newCall(request).enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        // Handle this
                        Log.e("Main","Try again latter!!!")
                    }
    
                    override fun onResponse(call: Call, response: Response) {
                        // Handle this
                        Log.e("Main",response.body!!.string())
                    }
                })
    

提交回复
热议问题