-1

I tried to use enqueue callback but it shows that as an error like "Type mismatch.

Required: Callback<*List<-Location>>"

and this is exactly what I wrote.

fun searchLocation(searchString: String){
    showProgress.value = true

    val retrofit = Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create())
        .build()

    val service = retrofit.create(WeatherNetwork::class.java)
    service.getLocation(searchString).enqueue(object : Callback<List<Location>>{
        override fun onFailure(call: Call<List<Location>>, t: Throwable) {
        }
        override fun onResponse(
            call: Call<List<Location>>,
            response: Response<List<Location>>
        ) {
         }
    })
}

May be it'll be more clear with the pic

UPD: WeatherNetwork

 const val BASE_URL = "/My API/"

 interface WeatherNetwork{

 @GET ("search?")
 fun getLocation(SearchString : String) : List<Location>
}
Midei
  • 1
  • 4

1 Answers1

0

Your WeatherNetwork is wrong, your api call needs to return a Call

change:

@GET ("search?")
 fun getLocation(SearchString : String) : List<Location>
}

To

@GET ("search?")
 fun getLocation(SearchString : String) : Call<List<Location>>
}

Side note:

I think you need to change your @GET("search?") to make it work.

For example if your REST URI is : localhost/search?st="wikipedia"

You need to update your function to:

@GET ("search")
 fun getLocation(@Query("st") SearchString : String) : List<Location>
}

have a look at : Retrofit 2 - URL Query Parameter

Biscuit
  • 4,840
  • 4
  • 26
  • 54