java - 将 JSON 数组从服务器 API 请求转换为 Retrofit2 中的对象

标签 java android json gson retrofit2

我有一个服务器,我可以在其中查询以获取论坛中的主题列表。通过curl返回的数据是这样的 -

[
  {
    "id": 728,
    "date": "2016-01-01T13:01:51",
    "date_gmt": "2016-01-01T07:31:51",
    ....
  },
  {
    "id": 556,
    "date": "2015-06-07T21:16:59",
    "date_gmt": "2015-06-07T15:46:59",
    ....
  },
  {
    "id": 554,
    "date": "2015-06-07T21:16:28",
    "date_gmt": "2015-06-07T15:46:28",
    ....
  }
]

这里每个JSONObject是有关论坛主题的数据。

{
  "id": 554,
  "date": "2015-06-07T21:16:28",
  "date_gmt": "2015-06-07T15:46:28",
  ....
}

我想在 ListView 中显示此数据在 Android 应用程序中。

但是,由于我使用的是 retrofit2gsonListView 创建对象,我总是得到的回复是 Not Found

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(ENDPOINT)
        .addConverterFactory(buildGsonConverter())
        .build();

serverAPI = retrofit.create(ServerAPI.class);

private Converter.Factory buildGsonConverter() {
    return GsonConverterFactory.create();
}

Call<List<Forum>> call = App.serverAPI.getListOfForums();
call.enqueue(new Callback<List<Forum>>() {
    @Override
    public void onResponse(Call<List<Forum>> call, Response<List<Forum>> response) {
        Log.i(TAG, "onResponse: " + (null != response.message() ? response.message() : ""));
        Log.i(TAG, "response body - " + (null != response.body() ? response.body() : ""));
        if (response.isSuccessful()) {
            adapter.setForums(response.body());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailure(Call<List<Forum>> call, Throwable t) {
        Log.i(TAG, "onFailure: " + t.toString());
        Log.i(TAG, "onFailure: " + t.getMessage());
    }
});

ServerAPI.class - 

@GET("/forum/")
Call<List<Forum>> getListOfForums();

现在,我在反序列化返回的 JSON 时没有执行任何操作。即使我使用 JsonDeserializer ,我应该如何处理才能更容易地填充 List<Forum> .

最佳答案

您不需要更改 JSON 本身。

public interface ServerAPI {
   @GET("/forum")
   Call<Forum> getListOfForums();
 }

FormResponse.java

public class ForumResponse {
  @SerializedName("id")
  private int id;

  @SerializedName("date")
  private String date;

  @SerializedName("date_gmt")
  private String date_gmt;
}
<小时/> MainActivity.java 中的

onResponse

@Override
public void onResponse(Call<Forum> call, Response<Forum> response) {
    String jsonString = response.body().toString();
    Log.i("onResponse", jsonString);
    Type listType = new TypeToken<List<ForumResponse>>() {}.getType();
    List<ForumResponse> yourList = new Gson().fromJson(jsonString, listType);
    Log.i("onResponse", yourList.toString());
}

关于java - 将 JSON 数组从服务器 API 请求转换为 Retrofit2 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36809497/

相关文章:

java - RocketMQ管理控制台

java - 多种方法的 SOAP 到 ReSTLes 策略

java - Servlet 类未找到异常

android - 升级到 AppCompat v22.1.0 现在按下菜单键时不会触发 onKeyDown 和 onKeyUp

java - 使用 Gson 解析不同 JSON 对象的列表

json - 将JSON数据添加到列表中时出错

json - 想要从 Laravel JSON 输出中删除像 "headers","original","exception"这样的意外对象

Java - 按升序获取 int 数组的索引

android cardview 显示卡周围的边框

java - 创建位图 NullPointerException