首页 > 解决方案 > 无法使用 volley 将 JSON 值从服务器获取到 Spinner

问题描述

以下是我的 JSON 数据,但我无法使用 volley获取"state"价值-Spinner

    [
    {
        "state_id": "1",
        "state": "Andaman and Nicobar (AN)"
    },
    {
        "state_id": "2",
        "state": "Andhra Pradesh (AP)"
    },
    {
        "state_id": "3",
        "state": "Arunachal Pradesh (AR)"
    },
    {
        "state_id": "4",
        "state": "Assam (AS)"
    },
    {
        "state_id": "5",
        "state": "Bihar (BR)"
    },
    {
        "state_id": "6",
        "state": "Chandigarh (CH)"
    },
    {
        "state_id": "7",
        "state": "Chhattisgarh (CG)"
    },
    {
        "state_id": "8",
        "state": "Dadra and Nagar Haveli (DN)"
    },
    {
        "state_id": "9",
        "state": "Daman and Diu (DD)"
    },
    {
        "state_id": "10",
        "state": "Delhi (DL)"
    },
    {
        "state_id": "11",
        "state": "Goa (GA)"
    },
    {
        "state_id": "12",
        "state": "Gujarat (GJ)"
    },
    {
        "state_id": "13",
        "state": "Haryana (HR)"
    },
    {
        "state_id": "14",
        "state": "Himachal Pradesh (HP)"
    },
    {
        "state_id": "15",
        "state": "Jammu and Kashmir (JK)"
    },
    {
        "state_id": "16",
        "state": "Jharkhand (JH)"
    },
    {
        "state_id": "17",
        "state": "Karnataka (KA)"
    },
    {
        "state_id": "18",
        "state": "Kerala (KL)"
    },
    {
        "state_id": "19",
        "state": "Lakshdweep (LD)"
    },
    {
        "state_id": "20",
        "state": "Madhya Pradesh (MP)"
    },
    {
        "state_id": "21",
        "state": "Maharashtra (MH)"
    },
    {
        "state_id": "22",
        "state": "Manipur (MN)"
    },
    {
        "state_id": "23",
        "state": "Meghalaya (ML)"
    },
    {
        "state_id": "24",
        "state": "Mizoram (MZ)"
    },
    {
        "state_id": "25",
        "state": "Nagaland (NL)"
    },
    {
        "state_id": "26",
        "state": "Odisha (OD)"
    },
    {
        "state_id": "27",
        "state": "Puducherry (PY)"
    },
    {
        "state_id": "28",
        "state": "Punjab (PB)"
    },
    {
        "state_id": "29",
        "state": "Rajasthan (RJ)"
    },
    {
        "state_id": "30",
        "state": "Sikkim (SK)"
    },
    {
        "state_id": "31",
        "state": "Tamil Nadu (TN)"
    },
    {
        "state_id": "32",
        "state": "Tripura (TR)"
    },
    {
        "state_id": "33",
        "state": "Uttar Pradesh (UP)"
    },
    {
        "state_id": "34",
        "state": "Uttarakhand (UK)"
    },
    {
        "state_id": "35",
        "state": "West Bengal (WB)"
    }
]

标签: android

解决方案


我不确定是什么错误,但我建议使用 Retrofit Library。它非常简单,整洁且性能明智,然后凌空抽射也很好。

它会自动将 json 转换为 pojo,所以我认为不需要手动获取

看看下面的例子。花一点时间在这上面,那么它对你将来会有帮助。

https://medium.com/@prakash_pun/retrofit-a-simple-android-tutorial-48437e4e5a23


推荐阅读