本文介绍了回调中的改造错误 2.0.2 beta 错误不会覆盖抽象方法 onResponse(Response<JsonElement>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    public class RetroFitClient
    {
    public static APIClass GetRetroFitClient()
        {
            return RETROFIT_API_CLASS;
        }

        public static void InitialiseRetroFitClient()
        {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(APP_BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            APIClass service = retrofit.create(APIClass.class);
        }

    }

public interface APIClass
{
    @POST("/zxx/")
    Call<JsonElement> GetClientAuthentication(String jArray);
}

    public void Call()
        {
            Call<JsonElement> call = RetroFitClient.GetRetroFitClient().GetClientAuthentication(my_content);
            call.enqueue(new Callback<JsonElement>() {
                @Override
                public void onResponse(Response<JsonElement> response, Retrofit retrofit) {
                    Log.d("onResponse" ,response.toString() );
                }

                @Override
                public void onFailure(Throwable throwable) {
                    throwable.printStackTrace();

                }
            });

        }

第一个是我的 RetrofitClient 类,其中正在初始化改造.第二个是我的 APIClass 包含函数声明.第三个是从我的 Activity 调用函数.

First one is my RetrofitClient class where retrofit is initializing.Second one is my APIClass containing the function declaration.Third one is the calling the function from my Activity .

But i am getting compile error of "is not abstract and does not override abstract method onResponse(Response<JsonElement>) in Callback"

和方法不会覆盖或实现来自超类型的方法".

and "method does not override or implement a method from a supertype".

Can anybody help on this?
Thanks in advance.

推荐答案

RETROFIT_API_CLASS 应该是一个接口,这些方法是由 Retrofit 框架自动实现的,你不应该直接调用它们,那是错误的原因.

The RETROFIT_API_CLASS should be an interface, those methods are implemented automatically by the Retrofit framework, you shouldn't call them directly, that's the reason of your error.

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(APP_BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .build();

APIClass service = retrofit.create(APIClass.class);

Call<YourParsedResponse> myCall = service.myCall();
myCall.enqueue(...)

顺便说一句,它可能对这个资源有所帮助,这是一个基本的工作项目,使用 Retrofit 2 进行非常简单的 http 调用

Btw it may help this resource, a basic working project with a very simple http call with Retrofit 2

https://github.com/saulmm/Retrofit-2-basic-sample

这篇关于回调中的改造错误 2.0.2 beta 错误不会覆盖抽象方法 onResponse(Response<JsonElement>)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:26