无法在Android

时间:2016-02-20 07:08:55

标签: android retrofit retrofit2

当我调用我的接口getRepo的方法时,它显示错误服务方法无法返回void for method Github.getRepo当我运行并检查调试它在interfac调用时崩溃我做了getRepo方法



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;
    }




}
&#13;
this is my interface and its calling class

public interface Github {

    @GET("/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;
    }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您是否已将okHttp客户端依赖项包含在您的项目中,并且还需要使用Rx封装您的api请求或使用RxJava适配器的Observable 这是你如何通过电话

来做到这一点

所以在你的情况下界面应该是

 @GET("/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通话 您可以阅读这些教程以获取更多信息 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