正确保存json响应

我想取一个json响应并保存到模型中,但我遇到了麻烦,这是我现在的代码。

Call<Forum> call = apiInterface.getForums();
call.enqueue(new Callback<Forum>() {

    @Override
    public void onResponse(Call<Forum> call, Response<Forum> response) {
        if (!response.isSuccessful()) {
            //show error
        }

        Forum forum = response.body();

        //both of these return null
        System.out.println("id: " + forum.getId());
        System.out.println("theme: " + forum.getTheme());

    }

    @Override
    public void onFailure(Call<Forum> call, Throwable t) {
       //show error
    }
});

这是json响应,我只对主对象感兴趣,组对象里面的所有东西我都不需要保存,我只想保存 id, theme, description, anonymous, start_date, end_date 目前,我不需要图像或视频,但也许在未来我会,我不知道如何将它们保存在模型中。

{
  "forums": [
    {
      "id": 1,
      "theme": "new forum",
      "description": "<p>adsadsfds asd adsa <strong>dfasdfa <\/strong>s fsda<\/p>",
      "user_id": 1,
      "anonymous": 0,
      "start_date": "2020-04-12 12:00:00",
      "end_date": "2020-04-30 12:00:00",
      "image": null,
      "video": null,
      "created_at": "2020-04-20 11:01:52",
      "updated_at": "2020-04-20 11:01:52",
      "group": [
        {
          "id": 1,
          "name": "Grupo 1",
          "user_id": 1,
          "course_id": 1,
          "major_id": 1,
          "period_id": 2,
          "classroom_id": 1,
          "created_at": null,
          "updated_at": null,
          "pivot": {
            "discussion_forum_id": 1,
            "group_id": 1
          },
          "users": [
            {
              "id": 1,
              "name": "sadmin",
              "lastname": "apellido",
              "maidenname": "apellido2",
              "card": "1111",
              "scard": "123",
              "user_type_id": 1,
              "email": "sadmin@gmail.com",
              "created_at": "2020-03-20 10:28:36",
              "updated_at": "2020-03-20 10:28:36",
              "pivot": {
                "group_id": 1,
                "user_id": 1
              }
            }
          ]
        },
        {
          "id": 2,
          "name": "Grupo 2",
          "user_id": 1,
          "course_id": 2,
          "major_id": 2,
          "period_id": 2,
          "classroom_id": 4,
          "created_at": null,
          "updated_at": null,
          "pivot": {
            "discussion_forum_id": 1,
            "group_id": 2
          },
          "users": [
            {
              "id": 2,
              "name": "admin",
              "lastname": "apellido",
              "maidenname": "apellido2",
              "card": "2222",
              "scard": "234",
              "user_type_id": 2,
              "email": "admin@gmail.com",
              "created_at": "2020-03-20 10:28:37",
              "updated_at": "2020-03-20 10:28:37",
              "pivot": {
                "group_id": 2,
                "user_id": 2
              }
            }
          ]
        }
      ]
    }
  ]
}

这就是论坛的POJO模型

 @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("theme")
    @Expose
    private String theme;
    @SerializedName("description")
    @Expose
    private String description;
    @SerializedName("anonymous")
    @Expose
    private Integer anonymous;
    @SerializedName("start_date")
    @Expose
    private String startDate;
    @SerializedName("end_date")
    @Expose
    private String endDate;

    //Setters and Getters here

REtrofit2客户端

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        Gson gson = new GsonBuilder().serializeNulls().setLenient().create();

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @NotNull
                    @Override
                    public okhttp3.Response intercept(@NotNull Chain chain) throws IOException {
                        Request originalRequest = chain.request();
                        Request newRequest = originalRequest.newBuilder()
                                .header("Authorization", "Bearer " + token)
                                .build();

                        return chain.proceed(newRequest);
                    }
                })
                .addInterceptor(interceptor)
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.0.2:8000/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

解决方案:

看来你的JSON响应返回的是List.请按以下方式尝试。

   Call<List<Forum>> call = apiInterface.getForums();
    call.enqueue(new Callback<List<Forum>>() {

        @Override
        public void onResponse(Call<List<Forum>> call, Response<List<Forum>> response) {
            if (!response.isSuccessful()) {
                //show error
            }

            List<Forum> forums = response.body();

            forums.forEach(forum -> {
                System.out.println("id: " + forum.getId());
                System.out.println("theme: " + forum.getTheme());
            });

        }

        @Override
        public void onFailure(Call<Forum> call, Throwable t) {
           //show error
        }
    });

给TA打赏
共{{data.count}}人
人已打赏
解决方案

我如何在使用cPanel的实时服务器上部署一个具有独立的前端(angular)和后端(spring boot)的应用程序?

2022-5-14 10:00:38

解决方案

在v-data-table中添加超链接 Vuetify公司简介

2022-5-14 11:00:09

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索