有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android改造:缺少方法体或声明抽象

我正在编写一个安卓应用程序,它将使用改型来发出API请求

我有这样一个助手类:

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

引用我的API接口类

知道发生了什么吗


共 (2) 个答案

  1. # 1 楼答案

    请尝试public interface获取您的API声明

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

    另外,看起来您正在创建ApidentPointInterface,然后才告诉构建器将日志级别设置为full

    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;
    }
    
  2. # 2 楼答案

    如果您更新到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