遍历全国省市数据

全国所有省市县的数据都是从服务器端获取到的,因此和服务器的交互是必不可少的

在util包下先增加一个HttpUtil类,代码如下所示:

public class HttpUtil {public static void sendHttpRequest(final String address, final HttpCallbackListener listener) {// 开启线程来发起网络请求new Thread(new Runnable() {@Overridepublic void run() {HttpURLConnection connection = null;try {URL url = new URL(address);connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET"); // GET表示希望从服务器那里获取数据connection.setConnectTimeout(8000); // 设置连接超时的毫秒数connection.setReadTimeout(8000); // 设置读取超时的毫秒数InputStream in = connection.getInputStream(); // 获取服务器返回的输入流// 下面对获取到的输入流进行读取BufferedReader reader = new BufferedReader(new InputStreamReader(in));StringBuilder response = new StringBuilder();String line;while ((line = reader.readLine()) != null) {response.append(line);}if (listener != null) {// 回调onFinish()方法listener.onFinish(response.toString());}} catch (Exception e) {if (listener != null) {// 回调onError()方法listener.onError(e);}} finally {if (connection != null) {connection.disconnect(); // 将这个HTTP连接关闭掉}}}}).start();}}


HttpUtil类中使用到了HttpCallbackListener接口来回调服务返回的结果,因此我们还需要在util包下添加这个接口,如下所示:

public interface HttpCallbackListener {void onFinish(String response);void onError(Exception e);}


由于服务器返回的省市县数据都是“代号|城市”格式的,所有我们最好再提供一个工具类来解析和处理这种数据

在util包下新建一个Utility类,代码如下所示:


public class Utility {/** * 解析和处理服务器返回的省级数据 */public synchronized static boolean handleProvincesResponse(CoolWeatherDB coolWeatherDB, String response) {if (!TextUtils.isEmpty(response)) {String[] allProvinces = response.split(",");if (allProvinces != null && allProvinces.length > 0) {for (String p : allProvinces) {String[] array = p.split("\\|");Province province = new Province();province.setProvinceCode(array[0]);province.setProvinceName(array[1]);// 将解析出来的数据存储到Province表coolWeatherDB.saveProvince(province);}return true;}}return false;}/** * 解析和处理服务器返回的市级数据 */public static boolean handleCitiesResponse(CoolWeatherDB coolWeatherDB, String response, int provinceId) {if (!TextUtils.isEmpty(response)) {String[] allCities = response.split(",");if (allCities != null && allCities.length > 0) {for (String c : allCities) {String[] array = c.split("\\|");City city = new City();city.setCityCode(array[0]);city.setCityName(array[1]);city.setProvinceId(provinceId);// 将解析出来的数据存储到City表coolWeatherDB.saveCity(city);}return true;}}return false;}/** * 解析和处理服务器返回的县级数据 */public static boolean handleCountiesResponse(CoolWeatherDB coolWeatherDB, String response, int cityId) {if (!TextUtils.isEmpty(response)) {String[] allCounties = response.split(",");if (allCounties != null && allCounties.length > 0) {for (String c : allCounties) {String[] array = c.split("\\|");County county = new County();county.setCountyCode(array[0]);county.setCountyName(array[1]);county.setCityId(cityId);// 将解析出来的数据存储到County表coolWeatherDB.saveCounty(county);}return true;}}return false;}}


需要准备的工具类就这么多

更多相关文章

  1. 一句话锁定MySQL数据占用元凶
  2. android操作SQLite
  3. Android数据的四种存储方式SharedPreferences、SQLite、Content
  4. Android(安卓)数据存储之 文件存储
  5. Android(安卓)Intent 机制学习
  6. 常用的Android常量
  7. android studio使用database navigator查看数据库
  8. Android(安卓)SQLite使用
  9. Android(安卓)ApiDemos示例解析(52):Graphics->AnimateDrawables

随机推荐

  1. [置顶] Android(安卓)View 仿iOS SwitchB
  2. Android弹出键盘布局闪动原理和解决
  3. android List拖动时背景为黑色问题
  4. DIY新浪微博Android手机客户端(一)(二)(三)完
  5. php7 list()、session及其他模块的修改实
  6. MacOS下PHP7.1升级到PHP7.4.15的方法
  7. php7 错误处理机制修改实例分析
  8. php7 list()、session及其他模块的修改实
  9. php中get_object_vars()在数组的实例用法
  10. jsp使用sessionScope获取session案例详解