2016-02-20 21 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