【问题标题】:Retrofit 2 returns null body改造 2 返回空体
【发布时间】: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 看起来不错

标签: android rest retrofit2


【解决方案1】:

我在浏览器中访问了https://api.github.com/search/repositories?q=threetenabp,我看到的 JSON 结构与您预期的不同:

{
  "total_count": 4,
  "incomplete_results": false,
  "items": [
    {
      "id": 38503932,
      "name": "ThreeTenABP",
      ...
    },
    {
      "id": 61799973,
      "name": "ThreeTenABPSample",
      ...
    },
    {
      "id": 78114982,
      "name": "TwitterPack",
      ...
    },
    {
      "id": 76958361,
      "name": "ZonedDateTimeProguardBug",
      ...
    }
  ]
}

在你的代码中,我看到了这个调用:

@GET("/search/repositories")
Call<List<Repository>> getByKeywords(@Query("q") String q);

如果收到的 JSON 响应的结构如下:

[
  { "id": ... },
  { "id": ... }
]

换句话说,在真正的 JSON 响应中,"items" 字段就是您所说的 List&lt;Repository&gt;,但您需要一些东西来表示顶级响应对象。

我会创建一个像这样的新类:

public class GithubResponse {

    private int total_count;
    private boolean incomplete_results;
    private List<Repository> items;
}

然后我会将您客户端界面中的调用更改为:

@GET("/search/repositories")
Call<GithubResponse> getByKeywords(@Query("q") String q);

【讨论】:

  • 感谢您更正了“按关键字获取”部分。但是“通过用户名获取”部分仍然返回一个空正文。当我测试api.github.com/users/test/repos 时,它直接返回一个数组。所以我看不出这个错误在哪里......
猜你喜欢
  • 2017-01-15
  • 2016-02-18
  • 1970-01-01
  • 2021-05-02
  • 2016-05-27
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
  • 2019-06-20
相关资源
最近更新 更多