im trying to make AndroidApp, that do POST requests to my springboot app, get responses, and than i will work with it and make features on results of response. I used retrofit2 to connect my AndroidApp with spring app, so i can send requests, but information that i need is in response body. Request is a password, if it true, spring app give body with info inside folder on my PC. When i try to get it, i have this body results:
body = com.example.myapp.LoginResponse@14d69ff
I think, that body response gone to response class, but im not sure about it. Can you please help me with it, because im really noob in retrofit and cant solve this problem for a week already?
API interface :
package com.example.myapp
import retrofit2.Call
import retrofit2.http.*
interface RetrofitAPI {
val token: String
@Headers("Content-Type: application/json")
@POST("lists")
fun createPost(
@Body dataModal: DataModal,
): Call<LoginResponse>
DataModal.kt :
package com.example.myapp
class DataModal {
val password = "123"
}
LoginResponse.kt:
package com.example.myapp
import com.google.gson.annotations.SerializedName
class LoginResponse (
@SerializedName("smth1")
var smth1: String = "",
@SerializedName("smth2")
val smth2: String = "",
@SerializedName("smth3")
val smth3: Int = 0
)
and MainActivity:
...
fun postData() {
val TEST_URL_API = "https://jsonplaceholder.typicode.com/"
val BASE_URL = "http://10.0.2.2:8080/api/"
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
val retrofitAPI = retrofit.create(RetrofitAPI::class.java)
val modal = DataModal()
val call = retrofitAPI.createPost(modal)
call.enqueue(object : Callback<LoginResponse> {
override fun onResponse(call: Call<LoginResponse?>, response: Response<LoginResponse?>) {
if (response.body() != null) {
val responseFromAPI = response.body()
val printed = LoginResponse()
Log.i(TAG, "responce string1 = ${printed.smth1}")
Log.i(TAG, "responce string2 = ${printed.smth2}")
Log.i(TAG, "response int1 = ${printed.smth3}")
} else {
Log.i(TAG, "body = null")
}
override fun onFailure(call: Call<LoginResponse?>, t: Throwable) {
responseTV!!.text = "Error found is : " + t.message
}
})
...
in cmd spring and postman i got my response normally. In test API body gets the same. I feel that i do simple error, but cant find it.
well, little count of searching days, asking on this site and i get it. This is my solution, may be it can help to someone. First of all i add OkHttpClient. It gave to me information about response. I saw, that my response is came to me normally, but i can't work with it. So, next work - create normally Responce file. Now i can do it on Kotlin, so i got it. My new files:
MainActivity :
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
apiCall()
}
private fun getClient(): Retrofit? {
val TEST_URL_API = "http://localhost:8080/api/"
val interceptor = HttpLoggingInterceptor()
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
val client: OkHttpClient = OkHttpClient.Builder()
.addInterceptor(interceptor)
.retryOnConnectionFailure(true)
.build()
return Retrofit.Builder()
.baseUrl(TEST_API_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build()
}
private fun apiCall() {
val call = getClient()!!.create(Api::class.java).createUser(
DataModal()
)
call.enqueue(object : Callback<UserResponse> {
override fun onResponse(call: Call<UserResponse>, response: Response<UserResponse>) {
val peremennayaResponsa = response.body()
Log.i(TAG, "LOG AND RESULT: body = $peremennayaResponsa")
Log.i(TAG, "headersы = ${response.headers()}")
}
override fun onFailure(call: Call<UserResponse>, t: Throwable) {
Log.i(TAG, "Ошибка запроса = ${t.localizedMessage!!.toString()}")
Toast.makeText(this@MainActivity, t.localizedMessage!!.toString(),
Toast.LENGTH_SHORT).show()
}
})
...
Api.kt :
interface Api {
@Headers("Content-Type: application/json;charset=UTF-8")
@POST("map")
fun createUser(
@Body dataModal: DataModal
) : Call<UserResponse>
And much more intrsting, UserResponse.kt:
data class UserResponse(
val response: Any
)
Why it is intresting? So, my response habe raw body like {"error":null,"response":{"list":{"txtfiles":[{**info that i need**
, and when i ask Application to do log in var i got an error like Expected a string but was BEGIN_OBJECT
. But i solved it, given full response parameter Any, and now i will write my full response in .txt file, parse it and work with info that i needed.
Thank you for reading, and helping
Happy coding :)