【问题标题】:Is it possible to have more class into response class in Android Studio Retrofit2?是否有可能在 Android Studio Retrofit2 中有更多的类进入响应类?
【发布时间】:2019-12-14 00:30:36
【问题描述】:

我正在尝试发送 HTTP 请求并获取一些数据作为响应。通常,我只是为响应创建一个类,但现在我的响应在一个字段中包含 5 个类。通常,响应只包含几个字段(字符串或整数),但现在它包含类和列表。我在 Android Studio 中使用 Retrofit2。

{
"data": {
    “example”:31;
        },
        " example ": {
                   “example”:31;
     },
        " example ": {
            “example”:31;
        },
    }
},
"errors": false,
                       “example”:31;
       } }

【问题讨论】:

  • 响应是什么样的?你能提供一个示例 JSON 吗?
  • 我编辑了第一篇文章
  • 请提供正确的 json 格式并且它也包含错误
  • 您是否尝试过使用嵌套类和自己的列表创建这样的响应?它看起来并不像用嵌套对象解析 json 那样,只是 Google 结果中的第一个示例:stackoverflow.com/questions/32942661/…

标签: android httprequest retrofit2


【解决方案1】:

试试这个解决方案,我认为它可以帮助您解决查询 Retrofit and extracting nested API objects

【讨论】:

    【解决方案2】:

    尝试使用 Moshi 或 Gson 与 Retrofit 进行解析(首选 Moshi) - 有很多关于它的教程,例如:https://proandroiddev.com/moshi-with-retrofit-in-kotlin-%EF%B8%8F-a69c2621708b

    您还可以使用 Android Studio 插件,这使得为 GSON / Moshi 创建类的过程变得更加容易:JsonToKotlinClass (https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-)

    顺便说一句:确保您的示例 json 字符串实际上是正确创建的: https://jsonformatter.curiousconcept.com/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-06
      • 2023-02-25
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      相关资源
      最近更新 更多