Home > database >  Android Studio showing this error :Retrofi2 Android HTTP method annotation is required (e.g., @GET,
Android Studio showing this error :Retrofi2 Android HTTP method annotation is required (e.g., @GET,

Time:05-16

It keeps showing me this error in Android Studio, I am using retrofit 2, trying to get data from Yelp API and it keeps showing me this msg

java.lang.RuntimeException: Unable to start activity ComponentInfo{sf.alomari.yelp/sf.alomari.view.MainActivity}: java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.). for method StoresApi.searchStores

And this is the SearchStores Method

import io.reactivex.Single
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Query

interface StoresApi {

    @GET("businesses/search")
    fun getStore(): Single<List<Stores>>
  fun searchStores(
      @Header("Authorization")authHeader:String,
      @Query("term")searchTerm:String,
      @Query("location")location:String): Call<List<Stores>>



}

Thank you

CodePudding user response:

That's simply because you haven't annotated your searchStores function. There is this fun getStore(): Single<List<Stores>> function in between the GET annotation and searchStore function and looking at the endpoint, probably the GET annotation is for the searchStore function. You need to annotate each function separately.

I guess it should look like this:

interface StoresApi {

    @GET("your-endpoint")
    fun getStore(): Single<List<Stores>>

    @GET("businesses/search")
    fun searchStores(
      @Header("Authorization") authHeader: String,
      @Query("term") searchTerm: String,
      @Query("location") location: String
    ): Call<List<Stores>>

}

CodePudding user response:

Try this, You have missed the annotation for the second function,. Every function needs its own annotation.

@GET("businesses/search")    
fun searchStores(
  @Header("Authorization") authHeader: String,
  @Query("term") searchTerm: String,
  @Query("location") location: String
): Call<List<Stores>>
  • Related