Reputation: 25
I can output the I / okhttp.OkHttpClient:
structure as it appears in the pictures, but I cannot access it in any way. There have been many topics on this subject and it has been stated that this is the solution response.body().string();
but when I write this code I get a Syntax error with .string()
.
class RegisterPage : AppCompatActivity() {
val logging = HttpLoggingInterceptor()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register_page)
button.setOnClickListener {
val retrofit=
Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).client(getHttpClient()).baseUrl(Constants.BASE_URL).build()
val jsonPlaceholderApi=retrofit.create(JsonPlaceholderApi::class.java)
val userPost = Register(
name.text.toString(),
user.text.toString(),
pass.text.toString()
)
val call=jsonPlaceholderApi.sendRegister(userPost)
call.enqueue(object : Callback<Void> {
override fun onResponse(call: Call<Void>, response: Response<Void>) {
if(response.isSuccessful){
println("asd")
}else{
println(response.body())
}
}
override fun onFailure(call: Call<Void>, t: Throwable) {
println(t.printStackTrace())
}
})
}
}
fun getHttpClient(): OkHttpClient {
// logging.setLevel(HttpLoggingInterceptor.Level.BODY)
logging.level = HttpLoggingInterceptor.Level.BODY
return OkHttpClient.Builder()
.connectTimeout(300, TimeUnit.SECONDS)
.readTimeout(300, TimeUnit.SECONDS).addInterceptor(logging).build()
}
}
Upvotes: 0
Views: 740
Reputation: 21
The generic type of your Response
is Void
, you should change that to something like String
, because the Void type dictates that it has no response.
Upvotes: 0
Reputation: 115
This is Kotlin. I think you have to do it like this:
response.body()?.string()
Source: https://medium.com/@rohan.s.jahagirdar/android-http-requests-in-kotlin-with-okhttp-5525f879b9e5
Upvotes: 1