Android天气预报程序(三)
16lz
2021-01-26
遍历全国省市数据
全国所有省市县的数据都是从服务器端获取到的,因此和服务器的交互是必不可少的
在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();}}
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;}}
需要准备的工具类就这么多
更多相关文章
- 一句话锁定MySQL数据占用元凶
- android操作SQLite
- Android数据的四种存储方式SharedPreferences、SQLite、Content
- Android(安卓)数据存储之 文件存储
- Android(安卓)Intent 机制学习
- 常用的Android常量
- android studio使用database navigator查看数据库
- Android(安卓)SQLite使用
- Android(安卓)ApiDemos示例解析(52):Graphics->AnimateDrawables