2017-01-10 100 views
0

我是Android编程的初学者。刚刚开始。
现在我试图在Android和Tomcat服务器之间使用改造进行通信。
但每当我点击登录按钮,这个错误让我发疯。 改进错误:找不到改进注释。 (参数#1)

 
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上了,但未能找到一个矿..

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

+1

使用了'任何理由 'com.squareup.retrofit:改造:2.0.0-beta2'' VS'' com.squareup.retrofit2:改造:2.1.0''? – Egor

回答

1

你忘了注释改造方法的参数。请尝试以下

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

问题是PostJson postJson,每个前面的参数必须与改装注释关联。在你的情况下,这应该是你的发布请求的主体。

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

非常感谢:) – James