2016-02-20 23 views
0

當我調用我的inteface getRepo的方法它顯示一個錯誤服務方法無法返回void方法Github.getRepo當我運行並檢查調試它崩潰在interfac調用我讓我該getRepo方法無法使用retofit 2.0 Beta在android

my mainActivity mtehod to call the api is 
 

 
Github api = GitHubService.getService(); 
 
     api.getRepo("basil2style", new Callback<Repository>() { 
 
      @Override 
 
      public void onResponse(Call<Repository> call, Response<Repository> response) { 
 

 
      } 
 

 
      @Override 
 
      public void onFailure(Call<Repository> call, Throwable t) { 
 

 
      } 
 
     }); 
 

 

 
And my model class is 
 
    
 
    package functionapps.retrofitdemo.AsyncHelper; 
 

 
public class Repository { 
 

 
    public String name; 
 

 
    public Repository(String name) { 
 
     this.name = name; 
 
    } 
 

 
    public String getName() { 
 
     return name; 
 
    } 
 

 
    public void setName(String name) { 
 
     this.name = name; 
 
    } 
 

 

 

 

 
}
this is my interface and its calling class 
 

 
public interface Github { 
 

 
    @GET("https://stackoverflow.com/users/{user}") 
 
    public void getRepo(@Path("user") String user, Callback<Repository> response); 
 

 
} 
 

 

 

 
public class GitHubService { 
 
    private static String API_URL = "https://api.github.com"; 
 

 
    public static Github getService() { 
 
     Retrofit retrofit = new Retrofit.Builder() 
 
       .baseUrl(API_URL) 
 
       .build(); 
 
     Github github = retrofit.create(Github.class); 
 

 
     return github; 
 
    }

+0

請分享堆棧跟蹤和分享改裝的版本,以及...像β2或3 .... –

+0

com.squareup.retrofit2:改造:2.0.0-BETA4 –

+0

請分享堆棧跟蹤.. –

回答

1

您有加入也改造2你需要調用封裝API請求的okHttp客戶的依賴到你的項目或可觀察到的使用RxJava適配器 這裏是你如何能請致電

所以你的情況的界面應該是

@GET("https://stackoverflow.com/users/{user}") 
    public Call<Repository> getRepo(@Path("user") String user); 

和調用代碼應該像

Call<Repository> call = apiService.getUser(username); 
call.enqueue(new Callback<Repo>() { 
    @Override 
    public void onResponse(Response<Repo> response) { 
    } 

    @Override 
    public void onFailure(Throwable t) { 

    } 
}); 

有了叫你也可以取消正在進行的API調用 您可以閱讀這些教程更info https://github.com/square/retrofit/tree/master/samples/src/main/java/com/example/retrofit https://guides.codepath.com/android/Consuming-APIs-with-Retrofit

http://inthecheesefactory.com/blog/retrofit-2.0/en