【问题标题】:how to get the data in this Api using retrofit in android如何在 android 中使用改造来获取此 Api 中的数据
【发布时间】:2019-07-08 10:10:36
【问题描述】:

获取这些详细信息时出现空指针异常

                 "order": 8,
                "month": "November",
                "amount": "450",
                "given": "450",
                "date": "12-Sep-2018".

谁能告诉我如何使用retrofit获取这种JSON格式的数据?

 {
        "status": 1,
        "msg": "Data found Successfully..!",
        "data": [
            {
                "subject": "Tution Fee",
                "sequence": 1,
                "months": {
                    "7": {
                        "order": 7,
                        "month": "October",
                        "amount": "450",
                        "given": "450",
                        "date": "12-Sep-2018",
                        "status": "Paid"
                    },
                    "8": {
                        "order": 8,
                        "month": "November",
                        "amount": "450",
                        "given": "450",
                        "date": "12-Sep-2018",
                        "status": "Paid"
                    },
                    "9": {
                        "order": 9,
                        "month": "December",
                        "amount": "200",
                        "given": null,
                        "status": "Due"
                    },
                    "10": {
                        "order": 10,
                        "month": "January",
                        "amount": "450",
                        "given": null,
                        "status": "Due"
                    },
                    "11": {
                        "order": 11,
                        "month": "February",
                        "amount": "450",
                        "given": null,
                        "status": "Due"
                    },
                    "12": {
                        "order": 12,
                        "month": "March",
                        "amount": "450",
                        "given": null,
                        "status": "Due"
                    }
                }
            },
            {
                "subject": "Transport Fee",
                "sequence": 2,
                "months": {
                    "7": {
                        "order": 7,
                        "month": "October",
                        "amount": "400.00",
                        "given": "400",
                        "date": "12-Sep-2018",
                        "status": "Paid"
                    },
                    "8": {
                        "order": 8,
                        "month": "November",
                        "amount": "400",
                        "given": "400",
                        "date": "12-Sep-2018",
                        "status": "Paid"
                    },
                    "9": {
                        "order": 9,
                        "month": "December",
                        "amount": "400",
                        "given": null,
                        "status": "Due"
                    },
                    "10": {
                        "order": 10,
                        "month": "January",
                        "amount": "400.00",
                        "given": null,
                        "status": "Due"
                    },
                    "11": {
                        "order": 11,
                        "month": "February",
                        "amount": "400.00",
                        "given": null,
                        "status": "Due"
                    },
                    "12": {
                        "order": 12,
                        "month": "March",
                        "amount": "400.00",
                        "given": null,
                        "status": "Due"
                    }
                }
            },
            {
                "subject": "Other Fee",
                "sequence": 3,
                "months": {
                    "1": {
                        "order": 1,
                        "month": "Back_dues",
                        "amount": 300,
                        "given": 0,
                        "status": "Due"
                    }
                }
            },
            {
                "subject": "computer_test22",
                "sequence": 4,
                "months": {
                    "1": {
                        "order": 1,
                        "month": "April",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    },
                    "2": {
                        "order": 2,
                        "month": "May",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    },
                    "3": {
                        "order": 3,
                        "month": "June",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    },
                    "4": {
                        "order": 4,
                        "month": "July ",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    },
                    "5": {
                        "order": 5,
                        "month": "August",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    },
                    "6": {
                        "order": 6,
                        "month": "September",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    },
                    "7": {
                        "order": 7,
                        "month": "October",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    },
                    "8": {
                        "order": 8,
                        "month": "November",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    },
                    "9": {
                        "order": 9,
                        "month": "December",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    },
                    "10": {
                        "order": 10,
                        "month": "January",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    },
                    "11": {
                        "order": 11,
                        "month": "February",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    },
                    "12": {
                        "order": 12,
                        "month": "March",
                        "amount": "100",
                        "given": "0",
                        "status": "Due"
                    }
                }
            }
        ],
        "current_status": {
            "subject": "Current Month-(July)",
            "amount": 8750,
            "given": 1700,
            "remain": 7050,
            "status": "due"
        }
    }

【问题讨论】:

  • 您使用的是哪种语言,请发布一些迄今为止尝试过的信息。
  • 您是否浏览过这里的文档square.github.io/retrofit?这是不言自明的
  • “日期”成员的模型类的类型?

标签: android json retrofit2


【解决方案1】:

您可以从您用于改造响应模型的模型对象中执行此操作。比如 hashMap(),或者你可以使用类对象来替换“jsonObject”

【讨论】:

    【解决方案2】:

    使用 Gson 映射,这将是解决这个问题的简单方法。

    【讨论】:

      【解决方案3】:

      首先,根据响应类型使用序列化创建相关模型。 例如:

      @SerializedName("status")
          private String status;
      @SerializedName("msg")
          private String msg;
      

      之后,使用上述字符串创建默认构造函数和第二个构造函数。之后,您可以根据使用的值创建 getter 和 setter。

      public "ClassName"(
                      String status,
                      String msg) {
                  this.status = status;
                  this.msg = msg;
      }
      
      
      public String getstatus() {
              return status;
          }
      
      public String getmsg() {
              return msg;
          }
      
      public void setStatus(String status) {
              this.status = status;
          }
      
      public void setMsg(String msg) {
              this.msg = msg;
          }
      

      【讨论】:

        【解决方案4】:

        在这种情况下,我使用了 Gson 映射并完成了。

        例如:“主题”:“交通费”, “序列”:2, “月”:{ “7”:{

        @SerializedName("主题") 私有 ArrayList subectList;

        主体模型内部:

        @SerializedName("传输") 私有字符串传输;

        @SerializedName("序列") 私有 int 序列;

        @SerializedName("月") 私有ArrayList个;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-14
          • 2020-06-06
          • 2017-02-17
          • 2021-10-17
          • 1970-01-01
          • 2017-05-09
          相关资源
          最近更新 更多