gpt4 book ai didi

android - RetrofitError 异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:25 25 4
gpt4 key购买 nike

我有以下代码...

public interface GithubService {
@GET("/repos/{owner}/{repo}/contributors")
public List<Contributor> contributors(@Path("owner") String owner, @Path("repo") String repo);
}

RepositoresBusinessController.class

RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("https://api.github.com").build();
GithubService service = restAdapter.create(GithubService.class);
listContributors = service.contributors(username, "retrofit");
Log.d("List Contributors", listContributors+" ");

贡献者类

public class Contributor {
String login;
int contributions;
}

并且不要在 Log 中打印并且应用程序会退出......有一部分异常

 725-725/com.example.githubapiexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
code here`retrofit.RetrofitError
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)

有人知道为什么吗?谢谢

最佳答案

Retrofit 具有异步回调机制,无需使用 AsyncTask 或服务。更改您的界面以使用回调而不是返回类型——

public interface GithubService {
@GET("/repos/{owner}/{repo}/contributors")
public void contributors(@Path("owner") String owner, @Path("repo") String repo,
Callback<List<Contributor>> contributors);
}

你可以这样调用它——

service.contributors(username, project, new Callback<List<Contributor>>() {
@Override
public void success(List<Contributor> contributors, Response response) {
// got the list of contributors
}

@Override
public void failure(RetrofitError error) {
// Code for when something went wrong
}
});

关于android - RetrofitError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27141610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com