Android / Java改造:Class无法转换为Callback

时间:2015-10-16 10:34:22

标签: java android retrofit

我是改装的新手,我正在尝试对名为RootObject的对象进行json响应。我坚持的错误是:

  

"错误:(21,44)错误:不兼容的类型:NewsController不能      转换为回调>"

现在有人在我这里犯了错误吗?谢谢!

public class NewsController {
        public void getNews(){
            Retrofit retrofit = new Retrofit.Builder().baseUrl("apilink").addConverterFactory(GsonConverterFactory.create()).build();
            GetNewsService service = retrofit.create(GetNewsService.class);
            try {
                service.GetNewsItems().enqueue(this); //asynchronous
                Response<List<RootObject>> response = service.GetNewsItems().execute(); //synchronous
            }
            catch (IOException e){

            }
        }
}

用于放置数据的类:

public class RootObject implements Serializable {
   public ArrayList<Result> results ;
   public int nextId;

    public ArrayList<Result> getResults() { return results; }
    public int getNextId() { return nextId; }

    public String toString() {
        return String.format("JEEJ" + nextId);
    }
}

接口

public interface GetNewsService {
    @GET("/Articles")
    Call<List<RootObject>> GetNewsItems();
}

2 个答案:

答案 0 :(得分:3)

首先, 将您的界面更改为:

public interface GetNewsService { 
  @GET("/Articles") 
  void GetNewsItems(Callback<List<RootObject>> cb);
}

同时更改你的newsController类。

public class NewsController {
    private RestAdapter restAdapter;
    static final String API_URL = "[Enter your API base url here]";
    public void getNews(){
        OkHttpClient mOkHttpClient = new OkHttpClient();
        mOkHttpClient.setConnectTimeout(15000,TimeUnit.MILLISECONDS);
        mOkHttpClient.setReadTimeout(15000,TimeUnit.MILLISECONDS);
        restAdapter = new RestAdapter.Builder().setEndpoint(API_URL).setClient(new OkClient(mOkHttpClient)).setLogLevel(RestAdapter.LogLevel.FULL) .build();
        GetNewsService service = restAdapter.create(GetNewsService.class);

        Callback<List<RootObject> cb = new Callback<List<RootObject>>() {
          @Override 
          public void success(List<RootObject> rootObjectList, Response response) { 
             //whatever you want to do with the fetched news items
          } 

          @Override 
          public void failure(RetrofitError error) { 
            //whatever you want to do with the error
          } 
        };
        service.GetNewsItems(cb);    
    }
}

您需要在build.gradle中添加以下依赖项:

compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.squareup.okhttp:okhttp:2.4.0'

答案 1 :(得分:2)

@megh vidani的答案有效,但他让你将代码从Retrofit 2切换到Retrofit 1.以下是如何在Retrofit 2中进行操作。你需要回到原来的gradle设置等 - < / p>

public class NewsController {
    public void getNews(){
        Retrofit retrofit = new Retrofit.Builder().baseUrl("apilink").addConverterFactory(GsonConverterFactory.create()).build();
        GetNewsService service = retrofit.create(GetNewsService.class);
        service.GetNewsItems().enqueue(new Callback<List<RootObject>>()  {
            @Override
            public void onResponse(Response<List<RootObject>> response) { 
                // Handle your response
                // Note HTTP errors are delivered here, you can check
                // response.isSuccess() or response.code() to determine
                // HTTP failures
            }

            @Override
            public void onFailure(Throwable t) {
                // Network errors
            }
        });
   }
}