Home > Back-end >  Conflict between okHttp3 and Retrofit. I want to user the .headers() function of Retrofit but it giv
Conflict between okHttp3 and Retrofit. I want to user the .headers() function of Retrofit but it giv

Time:10-16

I'm pretty new on Kotlin language and I was using okHttp3 to connect with my other APIs and It was working fine, but I saw some comments and I start using retrofit instead of okHttp. The problem is, I need the response header to save in my Global class, but when I put response.header() it says that the type required is retrofit2.http.Headers and found okhttp3.Headers even I didn't import the okhttp modules on my class. There is any way to specify that I want to use the Retrofit headers?

Thats my globalClass

package com.example.appfacul
import android.app.Application
import retrofit2.http.Headers

class GlobalClass:Application() {
  var globalUserName = "test"
  var email = ""
  var id = ""
  var responseHeaders = Headers()}

Thats my other class that I want to get the Headers

override fun onResponse(
                call: Call<AutenticationResponse>,
                response: Response<AutenticationResponse>
            ) {
                    val globalClass = GlobalClass()
                    val resultHeader=response.headers()
                    globalClass.responseHeaders=resultHeader

Other Class imports

enter image description here

And that's the error that the ide shows

IDE error

CodePudding user response:

Replace import retrofit2.http.Headers with import okhttp3.Headers and var responseHeaders = Headers() with var responseHeaders = Headers.Builder().build()

CodePudding user response:

You can't use the retrofit2.http.Headers type for var responseHeaders = Headers() because it's annotation and it's used for another purpose, see the doc

Since the headers are key-values pairs then you can just use it like this var responseHeaders = Map<String, List<String>>

And just convert okhttp3.Headers to map:

val resultHeader = response.headers()
globalClass.responseHeaders = resultHeader.toMultimap()
  • Related