2015-06-03 86 views
3

我正在編寫一個將使用Retrofit來製作API請求的Android應用程序。Android翻新:缺少方法正文,或聲明摘要

我有一個輔助類是這樣的:

public class ApiService { 
    public static final String TAG = ApiService.class.getSimpleName(); 

    public static final String BASE_URL = "https://myapiurl.com"; 

    public static void testApi(){ 
     ApiEndpointInterface apiService = prepareService(); 
     apiService.ping(new Callback<Response>() { 
      @Override 
      public void success(Response apiResponse, retrofit.client.Response response) { 
       Log.e(TAG, apiResponse.toString()); 

      } 

      @Override 
      public void failure(RetrofitError error) { 
       Log.e("Retrofit:", error.toString()); 

      } 
     }); 

    } 

    private static ApiEndpointInterface prepareService() { 
     RestAdapter restAdapter = new RestAdapter.Builder() 
       .setEndpoint(BASE_URL) 
       .build(); 
     ApiEndpointInterface apiService = 
       restAdapter.create(ApiEndpointInterface.class); 

     restAdapter.setLogLevel(RestAdapter.LogLevel.FULL); 
     return apiService; 
    } 

} 

而且我的實際改造實現很簡單:

public class ApiEndpointInterface { 

    @GET("/v1/myendpoint") 
    void ping(Callback<Response> cb); 
} 

的問題是,我不能構建項目,我得到的錯誤:

Error:(12, 10) error: missing method body, or declare abstract 

引用我的ApiEndpointInterface類。

任何想法是怎麼回事?

回答

10

嘗試public interface爲您的API聲明。

public interface ApiEndpointInterface { 

    @GET("/v1/myendpoint") 
    void ping(Callback<Response> cb); 
} 

而且,看起來像你說的建設者將日誌級別設置爲滿之前創建ApiEndpointInterface。

private static ApiEndpointInterface prepareService() { 

    RestAdapter restAdapter = new RestAdapter.Builder() 
      .setEndpoint(BASE_URL) 
      .setLogLevel(RestAdapter.LogLevel.FULL); 
      .build(); 

    ApiEndpointInterface apiService = 
      restAdapter.create(ApiEndpointInterface.class); 

    return apiService; 
} 
1

在您更新到版本okHttp 2.4.0的情況下,你會得到空的身體異常的最新版本不再允許零長度的要求,在這種情況下,你將不得不使用以下語法

公共接口ApiEndpointInterface {

@GET("/v1/myendpoint") 
void ping(Callback<Response> cb, @Body String dummy); 

}

呼叫

ApiEndpointInterface apiService = 
      restAdapter.create(ApiEndpointInterface.class); 

apiService.ping(callback,""); 

參考號 https://github.com/square/okhttp/issues/751