Fala pessoal, to usando Retrofit2 com Kotlin e estou tentando realizar uma requisição POST onde será retornado um Json.
A requisição é feita e a parte do servidor funciona (cadastra o usuario), porem quando vou tratar o response, ele sempre retorna null (com código 406 Not Acceptable)
RetrofitConfig.kt
class RetrofitConfig(baseUrl: String) {
private val gson = GsonBuilder().create()
private val httpClient = OkHttpClient.Builder()
private val retrofitBuilder = Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(GsonConverterFactory.create(gson))
private var retrofit = retrofitBuilder.build()
fun loginService(): LoginService {
return createService(LoginService::class.java)
}
fun registerService(): RegisterService {
return createService(RegisterService::class.java)
}
fun <S> createService(serviceClass: Class<S>): S {
retrofitBuilder.client(httpClient.build())
retrofit = retrofitBuilder.build()
return retrofit.create(serviceClass)
}
RegisterService.kt & LoginService.kt
interface RegisterService {
@POST("registrar/")
fun registrar(@Body usuario: Usuario): Call<AuthResponse>
}
interface LoginService {
@POST("login/")
fun login(@Body usuario: Usuario): Call<AuthResponse>
}
Realizando a Requisição (tanto registrar quanto logar)
override fun logIn(usuario: Usuario) {
val call = RetrofitConfig(baseUrl).loginService().login(usuario)
call.enqueue(object : Callback<AuthResponse> {
override fun onFailure(call: Call<AuthResponse>?, t: Throwable?) {
Toast.makeText(this@AuthFragmentRouterActivity, "Sem conexão com a internet.", Toast.LENGTH_LONG).show()
}
override fun onResponse(call: Call<AuthResponse>?, response: Response<AuthResponse>?) {
val body = response?.body()
if (response?.isSuccessful == true) {
if (body!!.sucesso) {
usuario.pontuacao = body.pontuacao
val it = Intent(this@AuthFragmentRouterActivity, QuizFragmentRouterActivity::class.java)
it.putExtra("usuario", Gson().toJson(usuario))
startActivity(it)
finish()
} else Toast.makeText(this@AuthFragmentRouterActivity, body.mensagem, Toast.LENGTH_LONG).show()
} else Toast.makeText(this@AuthFragmentRouterActivity, "Ocorreu um erro.", Toast.LENGTH_LONG).show()
}
})
}
override fun signUp(usuario: Usuario) {
val call = RetrofitConfig(baseUrl).registerService().registrar(usuario)
call.enqueue(object : Callback<AuthResponse> {
override fun onFailure(call: Call<AuthResponse>?, t: Throwable?) {
Toast.makeText(this@AuthFragmentRouterActivity, "Sem conexão com a internet.", Toast.LENGTH_LONG).show()
}
override fun onResponse(call: Call<AuthResponse>?, response: Response<AuthResponse>?) {
val body = response?.body()
Log.d("Quiz", "Response: ${response?.code()}")
if (response?.isSuccessful == true) {
if (body!!.sucesso) {
val it = Intent(this@AuthFragmentRouterActivity, QuizFragmentRouterActivity::class.java)
it.putExtra("usuario", Gson().toJson(usuario))
startActivity(it)
finish()
} else Toast.makeText(this@AuthFragmentRouterActivity, body.mensagem, Toast.LENGTH_LONG).show()
} else Toast.makeText(this@AuthFragmentRouterActivity, "Ocorreu um erro.", Toast.LENGTH_LONG).show()
}
})
}