Matteo
Matteo

Reputation: 81

Retrofit HTTP method annotation is required

I'm using retrofit 2.1, and when i call the @GET it says

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

Now.. if I define @retrofit.http.GET it throws

No Retrofit annotation found. (parameter #1)

With the @POST it's ok, it's works!

That's my error log for the HTTP method...

11-15 12:11:20.423 25033-25506/com.app.garuda.ridernet E/UncaughtException: java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                at android.os.AsyncTask$3.done(AsyncTask.java:300)
                                                                                at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                                at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                at java.lang.Thread.run(Thread.java:841)
                                                                             Caused by: retrofit.RetrofitError: gitapi.getquery: HTTP method annotation is required (e.g., @GET, @POST, etc.).
                                                                                at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:400)
                                                                                at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
                                                                                at $Proxy0.getquery(Native Method)
                                                                                at com.app.garuda.ridernet.SignIn.getjson(SignIn.java:92)
                                                                                at com.app.garuda.ridernet.SignIn.access$100(SignIn.java:28)
                                                                                at com.app.garuda.ridernet.SignIn$download.doInBackground(SignIn.java:125)
                                                                                at android.os.AsyncTask$2.call(AsyncTask.java:288)
                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                                at java.lang.Thread.run(Thread.java:841) 
                                                                             Caused by: java.lang.IllegalArgumentException: gitapi.getquery: HTTP method annotation is required (e.g., @GET, @POST, etc.).
                                                                                at retrofit.RestMethodInfo.methodError(RestMethodInfo.java:107)
                                                                                at retrofit.RestMethodInfo.parseMethodAnnotations(RestMethodInfo.java:179)
                                                                                at retrofit.RestMethodInfo.init(RestMethodInfo.java:117)
                                                                                at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:294)
                                                                                at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240) 
                                                                                at $Proxy0.getquery(Native Method) 
                                                                                at com.app.garuda.ridernet.SignIn.getjson(SignIn.java:92) 
                                                                                at com.app.garuda.ridernet.SignIn.access$100(SignIn.java:28) 
                                                                                at com.app.garuda.ridernet.SignIn$download.doInBackground(SignIn.java:125) 
                                                                                at android.os.AsyncTask$2.call(AsyncTask.java:288) 
                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                                at java.lang.Thread.run(Thread.java:841) 

My interface:

 import okhttp3.ResponseBody;
 import retrofit2.Call;
 import retrofit2.http.Field;
 import retrofit2.http.FormUrlEncoded;
 import retrofit2.http.GET; 
 import retrofit2.http.POST;
 import retrofit2.http.Query;


 public interface Testapi {


    @GET("/getq.php")
    Call<Testmodel> getquery(
            @Query("sort") String order
    );


    @FormUrlEncoded
    @POST("/insert.php")
    Call<ResponseBody> insert(
            @Field("nick") String nick,
            @Field("mail") String mail
    );

}

And the method

private void getjson() {
    //Creating a rest adapter

    RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build();

    //Creating an object of our api interface
    Testapi api = adapter.create(Testapi.class);
    Call<Testmodel> call = api.getquery("a");

    call.enqueue(new Callback<Testmodel>() {
        @Override
        public void onResponse(Call<Testmodel> call, Response<Testmodel> response) {
            try {
                Testmodel obj = response.body();

                TextView txt = (TextView) findViewById(R.id.textView);
                txt.setText("ID: " + obj.getId() + "   nick: " + obj.getNick() + "   mail: " + obj.getMail());

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<Gitmodel> call, Throwable throwable) {
            Toast.makeText(SignIn.this, "FALLIMENTO",     Toast.LENGTH_LONG).show();
        }
    });


}

Gradle file

    apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "23.0.2"
defaultConfig {

    multiDexEnabled true
    applicationId "xxx"
    minSdkVersion 17
    targetSdkVersion 24
    versionCode 1
    versionName "0.1"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
 }



dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

   androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',  {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile files('libs/retrofit-2.1.0.jar')
compile files('libs/gson-2.7.jar')
compile files('libs/okhttp-3.4.1.jar')
compile files('libs/okio-1.11.0.jar')

compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.google.android.gms:play-services:9.2.1'
compile 'com.android.support:design:24.1.1'
compile 'com.android.support:multidex:1.0.1'
compile 'com.google.android.gms:play-services-maps:9.2.1'
testCompile 'junit:junit:4.12'
}



apply plugin: 'com.google.gms.google-services'

Upvotes: 1

Views: 1829

Answers (2)

Marcin W
Marcin W

Reputation: 353

First of all please check your imports in all clases which use Retrofit.

In your interface Testapi you use retrofit2 but in your method getjson() you're creating retrofit.RestAdapter instead of retrofit2.Retrofit which is definitly wrong.

Please resolve this issue and check if it is working.

PS. Here are very nice tutorials about migration from reftrofit 1.* to 2.*. Follow them and everything should working properly:

https://futurestud.io/tutorials/retrofit-2-upgrade-guide-from-1-9 https://inthecheesefactory.com/blog/retrofit-2.0/en

Why don't you use retrofit2 gradle dependencies directly from maven? - for example like in attached links.

Upvotes: 1

Aleksandr
Aleksandr

Reputation: 4936

@POST annotated method, must have parameter, marked as @Body
Example:

@POST("/user/mobile/verify")
VerifyCodeResponse verifyMobile(@Body VerifyRequest request);

Upvotes: 1

Related Questions