美文网首页
Android加载三级地址

Android加载三级地址

作者: 雪凌风 | 来源:发表于2017-06-06 15:49 被阅读0次

    我们在开发APP的时候,经常会遇到需要填写地址信息的情况。废话不多说,按照以下步骤实现三级地址的自动填写

    第一步:从网上下载一个三级地址的json文件,放入Android Studio项目的assets文件夹下。json文件的内容如下所示(以北京为例)

    "area": "010", 
            "cities": [
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110101", 
                    "level": 2, 
                    "name": "东城区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110102", 
                    "level": 2, 
                    "name": "西城区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110105", 
                    "level": 2, 
                    "name": "朝阳区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110106", 
                    "level": 2, 
                    "name": "丰台区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110107", 
                    "level": 2, 
                    "name": "石景山区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110108", 
                    "level": 2, 
                    "name": "海淀区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110109", 
                    "level": 2, 
                    "name": "门头沟区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110111", 
                    "level": 2, 
                    "name": "房山区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110112", 
                    "level": 2, 
                    "name": "通州区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110113", 
                    "level": 2, 
                    "name": "顺义区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110114", 
                    "level": 2, 
                    "name": "昌平区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110115", 
                    "level": 2, 
                    "name": "大兴区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110116", 
                    "level": 2, 
                    "name": "怀柔区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110117", 
                    "level": 2, 
                    "name": "平谷区", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110228", 
                    "level": 2, 
                    "name": "密云县", 
                    "prefix": "区"
                }, 
                {
                    "area": "010", 
                    "cities": [ ], 
                    "code": "110229", 
                    "level": 2, 
                    "name": "延庆县", 
                    "prefix": "区"
                }
            ], 
            "code": "110000", 
            "level": 1, 
            "name": "北京市", 
            "prefix": "市"
    

    第二步:根据自己的需要,创建一个java bean类,用来存放城市信息。

    public class CityInfo {
    
        //城市代码
        private String code;
        //城市名称
        private String name;
        //城市的下级城市
        private String cities;
    
        public CityInfo(String cities, String code, String name) {
            this.cities = cities;
            this.code = code;
            this.name = name;
        }
    
        public String getCities() {
            return cities;
        }
    
        public String getCode() {
            return code;
        }
    
        public String getName() {
            return name;
        }
    }
    

    第三步:提取json文件当中的内容

     //将json文件转为String类型
     public static String getJson(Context context) {
            StringBuilder stringBuilder = new StringBuilder();
            try {
                BufferedReader bf = new BufferedReader(new InputStreamReader(
                        context.getAssets().open(CityEntry.FILE_NAME)));
                String line;
                while ((line = bf.readLine()) != null) {
                    stringBuilder.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return stringBuilder.toString();
        }
    
    /**
         * 将JSON字符串转化为Adapter数据
         *
         * @param str  
         */
        public static List<CityInfo> setData(String str) {
            List<CityInfo> data = new ArrayList<>();
            try {
                JSONArray array = new JSONArray(str);
                int len = array.length();
    
                for (int i = 0; i < len; i++) {
                    JSONObject object = array.getJSONObject(i);
                    if ((object.has(CityEntry.KEY_CITY)) &&
                            (object.has(CityEntry.KEY_NAME)) &&
                            (object.has(CityEntry.KEY_CODE))) {
                        data.add(new CityInfo(
                                object.getString(CityEntry.KEY_CITY),
                                object.getString(CityEntry.KEY_CODE),
                                object.getString(CityEntry.KEY_NAME)));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return data;
        }
    
        private static class CityEntry {
            //json文件的文件名
            private static final String FILE_NAME = "city_code.json";
            //根据项目所需的键值
            private static final String KEY_CODE = "code";
            private static final String KEY_NAME = "name";
            private static final String KEY_CITY = "cities";
    
        }
    
    
    

    最后,将setData返回的list作为adapter的参数即可显示城市信息了。
    json文件可以去网上下载,如有需要可以留下邮箱。

    相关文章

      网友评论

          本文标题:Android加载三级地址

          本文链接:https://www.haomeiwen.com/subject/zyegfxtx.html