【发布时间】:2017-10-20 20:45:20
【问题描述】:
我正在尝试实现 Retrofits 展示的基本练习:对 github API 进行简单的 get 查询。我正在尝试获取存储库列表,但尽管结果的状态为 200,但正文为空。 github上有一些ticket抱怨同样的问题,看起来可能来自json转换器。我用gson。但我没有发现哪个代码是罪魁祸首......
这是我的依赖项:
dependencies {
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
存储库 POJO:
public class Repository {
private int id;
private String name;
private String full_name;
private String html_url;
// getters + setters + toString
}
客户端界面:
public interface GithubClient {
public static final String ENDPOINT = "https://api.github.com";
@GET("/users/{user}/repos")
Call<List<Repository>> getByUser(@Path("user") String user);
@GET("/search/repositories")
Call<List<Repository>> getByKeywords(@Query("q") String q);
}
它是如何初始化的:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(GithubClient.ENDPOINT)
.addConverterFactory(GsonConverterFactory.create())
.build();
githubClient = retrofit.create(GithubClient.class);
以及请求执行:
Call<List<Repository>> request = githubClient.getByUser(inputText);
request.enqueue(new Callback<List<Repository>>() {
@Override
public void onResponse(Call<List<Repository>> call, Response<List<Repository>> response) {
progress.setVisibility(View.GONE);
results.setText(response.toString());
}
@Override
public void onFailure(Call<List<Repository>> call, Throwable t) {
progress.setVisibility(View.GONE);
results.setText(t.getMessage());
}
});
【问题讨论】:
-
您是否尝试在 POJO 上使用注释 @Expose 和 @SerializedName("field_name")
-
你使用的是github v3 api吗?从你的角度来看,Everting 看起来不错