Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 363
Seop Avatar asked Nov 26 '25 14:11

Seop


2 Answers

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

@POST("/Attendance/login.jsp")
Call<PostJson> postLogin(@Body PostJson postJson);
like image 107
Much Overflow Avatar answered Nov 28 '25 04:11

Much Overflow


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);
like image 27
Blackbelt Avatar answered Nov 28 '25 03:11

Blackbelt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!