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

*爱你&永不变心* 提交于 2019-12-02 23:24:05

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'

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.

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'
murt

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'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!