15

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 Interceptor it's working. Or is there something wrong with my Gradle Dependencies..?

E/AndroidRuntime﹕ FATAL EXCEPTION: main
        java.lang.IllegalArgumentException
    : HTTP method annotation is required (e.g., @GET, @POST, etc.).
            for method APIService.getRegAccrDetails
                    at retrofit.Utils.methodError(Utils.java:177)
                    at retrofit.Utils.methodError(Utils.java:167)
                    at retrofit.RequestFactoryParser.parseMethodAnnotations(RequestFactoryParser.java:135)
                    at retrofit.RequestFactoryParser.parse(RequestFactoryParser.java:59)
                    at retrofit.MethodHandler.create(MethodHandler.java:30)
                    at retrofit.Retrofit.loadMethodHandler(Retrofit.java:151)
                    at retrofit.Retrofit$1.invoke(Retrofit.java:132)
                    at $Proxy0.getRegAccrDetails(Native Method)
                    at alvin.test.myapplication.MainActivity.liferayAccess(MainActivity.java:136)
                    at alvin.test.myapplication.MainActivity.access$000(MainActivity.java:28)
                    at alvin.test.myapplication.MainActivity$1.onClick(MainActivity.java:49)
                    at android.view.View.performClick(View.java:3511)
                    at android.view.View$PerformClick.run(View.java:14105)
                    at android.os.Handler.handleCallback(Handler.java:605)
                    at android.os.Handler.dispatchMessage(Handler.java:92)
                    at android.os.Looper.loop(Looper.java:137)
                    at android.app.ActivityThread.main(ActivityThread.java:4424)
                    at java.lang.reflect.Method.invokeNative(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:511)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                    at dalvik.system.NativeStart.main(Native Method)

Here is my API Service to be called

  @GET("Triu-services-portlet.regaccrdetails/get-all-reg-accr-details-by-num-branch-code/num/{num}/branch-code/{branch-code}")
    public Observable<List<RegAccrDetails>> getRegAccrDetails(@Path("num") String num, @Path("branch-code")String branchCode);

My OkHttpClient Interceptor

private static OkHttpClient createOkHttpClient() {
   String username = "test@liferay.com";
   String password = "TEST";
   String credentials = username + ":" + password;
   final String basic =
           "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.DEFAULT);//no_wrap



    OkHttpClient client = new OkHttpClient();
    client.interceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response = chain.proceed(chain.request());

            Request original = chain.request();

            // Customize the request
            Request request = original.newBuilder()
                    .header("Authorization", basic)
                    .header("Accept", "application/json")
                    //.header("Authorization", "auth-token")//add token for service A4oslsSXZxfbLdk
                    .method(original.method(), original.body())
                    .build();

            response = chain.proceed(request);

            // Customize or return the response
            return response;
        }
    });

   return client;
}

Here is my Call for my API

private void liferayAccess(){
    Log.d("liferayAccess", "Entered");
    APIService service =  ServiceGenerator.createService(APIService.class);
    Observable<List<RegAccrDetails>> liferayResponse = service.getRegAccrDetails("004589209", "001");

    liferayResponse.subscribeOn(Schedulers.newThread()).map(listResponse -> "response index 0 " + listResponse.get(0).getRegNum())
            .subscribe( response-> Log.d("Liferay Num", response),
                        error -> Log.d("Error", error.toString())
                    );
}

Here is my Gradle Dependencies

 compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'

compile 'io.reactivex:rxjava:1.0.16'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

//compile 'com.squareup.retrofit:converter-simplexml:2.0.0-beta2'
/*compile 'com.squareup.okhttp:okhttp:2.2.0'*/
compile ('com.squareup.retrofit2:retrofit:2.0.0-beta3') {//com.squareup.retrofit2:retrofit:2.0.0-beta3
    // exclude Retrofit’s OkHttp peer-dependency module and define your own module import
    //exclude module: 'okhttp'
}
compile 'com.squareup.okhttp3:okhttp:3.0.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'

Here is my App Gradle File

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }




    defaultConfig {
        applicationId "alvin.test.myapplication"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'

    compile 'io.reactivex:rxjava:1.0.16'
    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

    //compile 'com.squareup.retrofit:converter-simplexml:2.0.0-beta2'
    /*compile 'com.squareup.okhttp:okhttp:2.2.0'*/
    compile ('com.squareup.retrofit2:retrofit:2.0.0-beta3') {//com.squareup.retrofit2:retrofit:2.0.0-beta3
        // exclude Retrofit’s OkHttp peer-dependency module and define your own module import
        //exclude module: 'okhttp'
    }
    compile 'com.squareup.okhttp3:okhttp:3.0.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'

    //compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.google.android.gms:play-services-gcm:7.3.0'
    compile 'com.google.android.gms:play-services:7.8.0'
}
retrolambda {
    jdk "C:\\Program Files\\Java\\jdk1.8.0_20"
}

My Proguard. I also tried add and remove it but same error log happen

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


-keep class com.google.gson.** { *; }
-keep class com.google.inject.** { *; }
-keep class org.apache.http.** { *; }
-keep class org.apache.james.mime4j.** { *; }
-keep class javax.inject.** { *; }
-keep class retrofit.** { *; }
DreamBigAlvin
  • 884
  • 3
  • 13
  • 35

5 Answers5

45

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.

enter image description here

Jraco11
  • 4,526
  • 3
  • 20
  • 20
12

Issue

You're using 2.0.0-beta2 versions of Retrofit plugins which depend on 2.0.0-beta2 version of Retrofit core which still lives in retrofit package and at com.squareup.retrofit coordinates.

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

Then you're importing 2.0.0-beta3 version of Retrofit core which lives in retrofit2 package and at com.squareup.retrofit2 coordinates. Basically, it can be used alongside 2.0.0-beta2 or even 1.x version.

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

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

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

Read more about why this happened.

Solution

Update the dependency and fix your imports.

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

See changelog.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
8

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'
Harsh Sharma
  • 898
  • 1
  • 14
  • 30
  • I'm not using proguard and i'm having the error an i also tried to add that but then.. same thing happened.. – DreamBigAlvin Feb 16 '16 at 10:06
  • @DreamBigAlvin have you checked your build.gradle file – Harsh Sharma Feb 16 '16 at 10:08
  • my minifyEnabled is false so it will not using the proguard.. i also tried implementing it using proguard but same thing happened i added those proguard rules and it's same error logs.. – DreamBigAlvin Feb 16 '16 at 10:10
  • @DreamBigAlvin have you tried adding the dependency mentioned in my answer – Harsh Sharma Feb 16 '16 at 10:36
  • I meet the same issue.After include your magic code ,it works! It is strange the [official website](http://square.github.io/retrofit/) does not offer this – Ninja Sep 06 '16 at 02:21
0

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'
Community
  • 1
  • 1
murt
  • 3,790
  • 4
  • 37
  • 48
0

In my case everything was perfect except I had imported wrong @POST (while importing by mistakely I created POST annotation in my project), when I removed it, it worked.

So my suggestion is, once check imports properly.

shashank J
  • 67
  • 7