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 = "345@6.com"
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