2015-10-16 41 views
1

我是新来翻新,我想要得到一个JSON响应的对象RootObject。我被卡住的错误是:Android/Java改造:类不能转换为回调

"Error:(21, 44) error: incompatible types: NewsController cannot be converted to Callback>"

现在有人在我的错误吗?谢谢你的问候!

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(); 
} 
+0

类名为NewsController必须实现回调接口 –

回答

2

首先, 你的界面改成这样:

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' 
+0

您好,感谢为C颂!你在建造等级(app)中导入了什么?在我的android工作室有很多错误 – DutchProgrammer

+0

这解决了几乎所有的错误。 '''' void GetNewsItems(回调<列表>); – DutchProgrammer

+0

哦,是的!我的错!编辑代码。看一看! –

1

@megh vidani的答案的作品,但他从改造2切换你的代码改造1.这里是如何做到在改造2.您将需要返回到原来的gradle这个设置等 -

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 
      } 
     }); 
    } 
} 
+0

好的!我不知道改造API的变化!感谢您指出了这一点! :) –

相关问题