Retrofit 2 HTTP method annotation is required (e.g., @GET, @POST, etc.)

后端 未结 4 783
耶瑟儿~
耶瑟儿~ 2021-02-04 09:01

What\'s wrong with my Retrofit configuration? I\'m having this error when I\'m adding Basic Authentication with my OkHttpClient but when I used the default client without Interc

4条回答
  •  梦如初夏
    2021-02-04 09:54

    As Eugen Pechanec stated the problem lies usually in conflict between retrofit and retrofit 2. In my case the error "HTTP method annotation is required @GET @POST" was caused by using wrong structure Builder of HTTPLoggingInterceptor.

    Hence make sure that you are using okhttp3 with retrofit2

    So the right structure looks like THIS:

    import okhttp3.OkHttpClient;
    import okhttp3.logging.HttpLoggingInterceptor;
    import retrofit2.Retrofit;
    import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
    import retrofit2.converter.gson.GsonConverterFactory;
    
    ...
    
    Retrofit provideRetrofit(){
    
    // get base url for endpoint
    String endpointUrl = BuildConfig.apiEndpointUrl;
    
    // add logging interceptor
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(logging).build();
    
    // build retrofit instance
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(endpointUrl)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
    
    return retrofit;
    }
    

    And the app/build.gradle

    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
    compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    

提交回复
热议问题