【问题标题】:How To Parse Response of Multiple Types of Single Key如何解析多种单键的响应
【发布时间】:2020-02-20 22:15:59
【问题描述】:

如何解析多种类型的响应?

Key 是这样的(假设 student_list 是 list 类型的 key,当 student_list 为空时,它会变成一个类似 student_list="" 的字符串),如何管理这个使用改造的响应类型?我正在使用 MVVM 模型进行改造。

我的回应:

当我将数据放入列表时

{
    "status": 200,
    "data": [
        {
            "prod_month_total": 2989.61,
            "product": "GAS"
        },
        {
            "prod_month_total": 39566.22,
            "product": "OIL"
        },
        {
            "prod_month_total": 83912.55,
            "product": "OTHER"
        }
    ]
}

当列表为空时响应:

{"status":404,"data":"No result found"}

我收到此错误:

java.lang.IllegalStateException: 应为 BEGIN_ARRAY,但在第 1 行第 23 列路径 $.data 处为 STRING

【问题讨论】:

  • 请添加一些代码示例,以便我们更好地理解。
  • 感谢@AtishAgarwal,这是我在将数据放入列表时从服务器获取的响应 { "status": 200, "data": [ { "prod_month_total": 2989.61, "product ": "GAS" }, { "prod_month_total": 39566.22, "product": "OIL" }, { "prod_month_total": 83912.55, "product": "OTHER" } ] },当列表为空时响应:{ "status":404,"data":"No result found"} ,我根据我的回复创建了模型类。
  • 这能回答你的问题吗? How to Handle Two Different Response in Retrofit

标签: android android-layout android-fragments


【解决方案1】:

首先创建正确的模型类使用此站点http://www.jsonschema2pojo.org

比使用

`if( reponce.isSucessful){

if(responce.status==200){
   //code here 
  }
else{
 // find the error
}
 }else
 {
//
 }`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2015-01-29
    相关资源
    最近更新 更多