coucou
coucou

Reputation: 633

Retrofit Error : No Retrofit annotation found. (parameter #1)

I'm a beginner in Android programming. just started.
Now I'm trying to communicate between Android and Tomcat server using retrofit.
but whenever I click login button, this error keeps me crazy.

java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #1)

Here are my errors..

java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #1)
                 for method NetworkService.postLogin
                           at com.example.ab.MainActivity.networkServiceModule(MainActivity.java:68)
                           at com.example.ab.MainActivity$1.onClick(MainActivity.java:50)

I added these in gradle

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

Interface :

public interface NetworkService {
    @POST("/Attendance/login.jsp")
    Call<PostJson> postLogin(PostJson postJson);
}

some part of MainActivity :

         ApplicationController application = ApplicationController.getInstance();
         application.buildNetworkService("xxx.xxx.xxx.xxx",8080);
         networkService = ApplicationController.getInstance().getNetworkService();

         login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ID = id.getText().toString();
                String Pwd = password.getText().toString();
                networkServiceModule(ID,Pwd);     //this is line 50.
            }
        });

public void networkServiceModule(String ID, String Pwd){
        Log.d("networkServiceModule","ID : "+ID);
        Log.d("networkServiceModule","PW : "+Pwd);

        Call<PostJson> thumbnailCall = networkService.postLogin(new PostJson(ID,Pwd));     //this is line 68.
        thumbnailCall.enqueue(new Callback<PostJson>() {
            @Override
            public void onResponse(Response<PostJson> response, Retrofit retrofit) {
                if(response.isSuccess()) {
                    String resultCode = response.body().getResult_code().toString();
                    Toast.makeText(getBaseContext(), "Login : " + resultCode, Toast.LENGTH_SHORT).show();
                } else {
                    int statusCode = response.code();
                    Log.d("networkServiceModule", "response Code : "+statusCode);
                }
            }

ApplicationController :

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;

public class ApplicationController extends Application {
    private static ApplicationController instance;
    public static ApplicationController getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        ApplicationController.instance = this;
    }

    private NetworkService networkService;
    public NetworkService getNetworkService() {
        return networkService;
    }
    private String baseUrl;

    public void buildNetworkService(String ip, int port) {
        synchronized (ApplicationController.class) {
            baseUrl = String.format("http://%s:%d", ip, port);
            Gson gson = new GsonBuilder()
                    .create();

            GsonConverterFactory factory = GsonConverterFactory.create(gson);

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(factory)
                    .build();
            networkService = retrofit.create(NetworkService.class);
        }
    }
}

I've kept trying to apply some solutions that I got from StackOverflow, but failed to find one for mine..

This is my first question on StackOverFlow, sorry for codes looking ugly.

Upvotes: 3

Views: 9781

Answers (4)

Ananth
Ananth

Reputation: 2735

Though this answer is not specifically related to question, I found myself here while solving the issue. I solved it, therefore sharing.

I was trying to use retrofit along with coroutines and getting error Retrofit Error : No Retrofit annotation found. (parameter #2) , even though there was no parameter #2.

/**
 * Suspend function to get media of the day for the day when this function is called.
 * Will return a Media object in the response.
 */
@GET("planetary/apod")
suspend fun getTodaysMedia(@Query("api_key") apiKey: String): Media

Solved this by upgrading the retrofit version: version_retrofit = "2.9.0"

Upvotes: 3

S Kranthi Kumar
S Kranthi Kumar

Reputation: 750

You forgot to annotate the retrofit method's parameters @Body like

@POST("/Attendance/login.jsp")
Call<PostJson> postLogin(@Body PostJson postJson);

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157457

the problem is with PostJson postJson, every former parameter has to be associated with a retrofit annotation. In your case that should be the body of your post request.

   Call<PostJson> postLogin(@Body PostJson postJson);

Upvotes: 3

Much Overflow
Much Overflow

Reputation: 3140

You forgot to annotate the retrofit method parameter. Try the following

@POST("/Attendance/login.jsp")
Call<PostJson> postLogin(@Body PostJson postJson);

Upvotes: 5

Related Questions