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

后端 未结 4 781
耶瑟儿~
耶瑟儿~ 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:32

    It look like you are using proguard and it is stripping annotations. To get saved from it add this lines to your proguard-rules.pro

    -keepattributes *Annotation*
    -keep class retrofit.** { *; }
    -keepclasseswithmembers class * {
    @retrofit.http.* <methods>; }
    -keepattributes Signature
    

    if not using proguard make sure that you havn't written something in your app build.gradle something like this

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    

    Note: Android does not come normally with many of the javax.annotation library by default.

    if it is not so then try adding this in your gradle dependency (build.gradle)

    provided 'org.glassfish:javax.annotation:10.0-b28'
    
    0 讨论(0)
  • 2021-02-04 09:37

    Using the wrong "@GET"

    This may help someone coming from retrofit1, I was getting this same error and the fix was simple. In my interface I was unaware that I was using @GET from retrofit.http and not @GET from retrofit2.http, changing the annotation from retrofit.http to retrofit2.http was all I needed to do.

    0 讨论(0)
  • 2021-02-04 09:50

    Issue

    You're using beta2 versions of retrofit plugins which depend on beta2 version of retrofit which still lives in com.squreup.retrofit package.

    compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
    

    Then you're importing beta3 version of retrofit itself which lives in retrofit2 package. Basically it can be used alongside beta2 version.

    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
    

    You're not really using beta3 at all, because it's incompatible with beta2 plugins and you'd get compile time errors. Check your imports to verify.

    What happened is (most likely) you use everything from com.square.retrofit package except for the @GET class which is from retrofit2 package. Despite their identical name these classes are not the same.

    Solution

    Move to beta4 and retrofit2 package. Fix your imports. Profit.

    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'
    
    0 讨论(0)
  • 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'
    
    0 讨论(0)
提交回复
热议问题