I am calling API using Retrofit in Android. and get response from POST method in form of ResponseBody.
As,my API call is as follow-
@POST("api/auth/store-login")
fun postLoginDetail(@Body userData: RequestBody):Call<ResponseBody>
Now,I want to convert the response of this call to JSONObject so I can use it in my code.
I did this -
retrofitCall.enqueue(object : Callback<ResponseBody?> {
override fun onResponse(call: Call<ResponseBody?>, response: Response<ResponseBody?>) {
Log.e("response","responseNew ${response.body().toString()}")
val responseBody = JSONObject("{${response.body().toString()}}")
}
but it is not working as Log.e returns -> responseNew okhttp3.ResponseBody$1@6015676
and hence it cannot be converted to JSON as response.body().to()String
is not String or in JSON format.
My actual response in PostMan is like - {"clients":["client1","client2"],"has_multiple_clients":true}
try replacing response.body().toString()
with response.body?.string()
but make sure you save the results, 'cause the .string()
clears the information after being called, making future calls on response.body return no results