Home > OS >  OkHttpClient get information from log
OkHttpClient get information from log

Time:08-02

i am try to make my app that send POST request on server, get response and work with it, but it get little hard to me, so i got a problem to get a response from OkHttpClient to local variable. My app work fine, it sending requests to server, gets a responses, but when i try to make it var i got link to local class. I can give to You my log screen: Log

As you can see from my logcat, i get a response, but when i try to use it in Var i got something like com.example.composetest.UserResponse@8f0d356 . I guess, problem is in UserResponse.java file, but not sure about it, so, can You tell me how i can realise response from OkHttpClient to var? Also i add my files as a code, so you can see how i made POST requests to server:

Main Activity:

...
private fun getClient(): Retrofit? {

        val TEST_URL_API = "https://jsonplaceholder.typicode.com/"

        val interceptor = HttpLoggingInterceptor()
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)

        val client: OkHttpClient = OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .retryOnConnectionFailure(true)
            .build()

        return Retrofit.Builder()
            .baseUrl(TEST_URL_API)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build()


    }

    private fun apiCall() {

        val firstname = "123"
        val lastname = "234"
        val email = "[email protected]"
        val username = "132456"
        val pw = "12345678"

        val call = getClient()!!.create(Api::class.java).createUser(
            firstname,
            lastname,
            email,
            username,
            pw
        )


        call.enqueue(object : Callback<UserResponse>  {
            override fun onResponse(call: Call<UserResponse>, response: Response<UserResponse>) {


                Log.v(TAG, "RESULTS response.body = ${response.body()}")
                
            }

            override fun onFailure(call: Call<UserResponse>, t: Throwable) {
                Toast.makeText(this@MainActivity, t.localizedMessage!!.toString(),
                    Toast.LENGTH_SHORT).show()
            }
        })

Api.kt

interface Api {

    @Headers("Content-Type: application/x-www-form-urlencoded")
    @POST("users")
    @FormUrlEncoded
    fun createUser(
        @Field("first_name") fName: String,
        @Field("last_name") lName: String,
        @Field("email") email: String,
        @Field("username") username: String,
        @Field("password") password: String,
    ) : Call<UserResponse>

}

UserResponse.java

public class UserResponse {
}

I think, problem is in Response file, but not sure about it. And also, i'm sorry if this question is already on site, i don't find something about it, if You saw something that can help give me link please. Thank you

CodePudding user response:

Yes, the problem is in the implementation of UserResponse class. In oder to see the response in Logcat you should override toString() method for your UserResponse class. Doing so in Java is quite straightforward, and you can easily find how to do it: How to override toString() properly in Java?

But, it's even more easier to do it in Koltin – just use data class. It's even more sensible, as you are already using Kotlin for other parts of your application. You can find details on how to use Kotlin classes in official documentation: https://kotlinlang.org/docs/data-classes.html

My recommendation would be make changes like this:

UserResponse.kt

data class UserResponse(
    val id: String, 
    // list there other fields you get in response and you need for your application
)
  • Related