改造错误:未找到改造注释。 (参数#1)

时间:2017-01-10 08:51:24

标签: android retrofit

我是Android编程的初学者。刚开始。
现在我正在尝试使用改造在Android和Tomcat服务器之间进行通信 但每当我点击登录按钮时,这个错误让我发疯。

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

这是我的错误..

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)

我在gradle中添加了这些

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

界面:

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

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);
        }
    }
}

我一直试图应用我从StackOverflow获得的一些解决方案,但未能找到我的解决方案..

这是我在StackOverFlow上的第一个问题,对于看起来丑陋的代码感到抱歉。

4 个答案:

答案 0 :(得分:2)

您忘记注释改装方法参数。请尝试以下

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

答案 1 :(得分:1)

问题在于PostJson postJson,每个以前的参数都必须与改装注释相关联。在您的情况下,应该是您的帖子请求的正文。

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

答案 2 :(得分:0)

您忘了对改造方法的参数@Body进行注释

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

答案 3 :(得分:0)

尽管此答案与问题没有特别联系,但我在解决问题时发现自己在这里。我解决了,因此分享。

即使没有参数#2,我仍试图与协程一起使用改造并出现错误Retrofit Error : No Retrofit annotation found. (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

通过升级改造版本来解决此问题: version_retrofit = "2.9.0"

相关问题