images/loading.gif' data-original="http://images2015.cnblogs.com/blog/771964/201509/771964-20150916094752914-667238149.jpg" width="151" height="244">
根据上次的内容
1. 界面布局
weather_layout.
<??><LinearLayout ="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#484E61" > <Button android:id="@+id/switch_city" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:background="@drawable/home" /> <TextView android:id="@+id/city_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="#fff" android:textSize="24sp" /> <Button android:id="@+id/refresh_weather" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:background="@drawable/refresh" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#27A5F9"> <TextView android:id="@+id/publish_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:textColor="#FFF" android:textSize="18sp" /> <LinearLayout android:id="@+id/weather_info_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical"> <TextView android:id="@+id/current_date" android:layout_width="wrap_content" android:layout_height="40dp" android:gravity="center" android:textColor="#FFF" android:textSize="18sp" /> <TextView android:id="@+id/weather_desp" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_gravity="center_horizontal" android:gravity="center" android:textColor="#FFF" android:textSize="40sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="60dp" android:layout_gravity="center_horizontal" android:orientation="horizontal"> <TextView android:id="@+id/temp1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textColor="#FFF" android:textSize="40sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:text="~" android:textColor="#FFF" android:textSize="40sp" /> <TextView android:id="@+id/temp2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textColor="#FFF" android:textSize="40sp" /> </LinearLayout> </LinearLayout> </RelativeLayout></LinearLayout>
2. 解析服务器返回的数据,存储到本地
Utility.java
import org.json.JSONException;import org.json.JSONObject;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;/** * Created by zps on 2015/9/12. */public class Utility { public synchronized static boolean handleProvincesResponse(CoolWeatherDB coolWeatherDB, String response) { //isEmpty用于判断""或null if (!TextUtils.isEmpty(response)) { String[] allProvinces = response.split(","); if (allProvinces != null && allProvinces.length > 0) { for (String p : allProvinces) { //split分离,'\\|'传给正则就是"\|",表示对|进行转义,不作为特殊字符使用 String[] array = p.split("\\|"); Province province = new Province(); //数据格式为"代号|城市",故array[0]为代号 province.setProvinceCode(array[0]); province.setProvinceName(array[1]); 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); 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); coolWeatherDB.saveCounty(county); } return true; } } return false; } public static void handleWeatherResponse(Context context, String response) { try { JSONObject jsonObject = new JSONObject(response); JSONObject weatherInfo = jsonObject.getJSONObject("weatherinfo"); String cityName = weatherInfo.getString("city"); String weatherCode = weatherInfo.getString("cityid"); String temp1 = weatherInfo.getString("temp1"); String temp2 = weatherInfo.getString("temp2"); String weatherDesp = weatherInfo.getString("weather"); String publishTime = weatherInfo.getString("ptime"); saveWeatherInfo(context, cityName, weatherCode, temp1, temp2, weatherDesp, publishTime); } catch (JSONException e) { e.printStackTrace(); } } /** * 将服务器返回的所有天气信息存储到SharedPreferences 文件中。 */ public static void saveWeatherInfo(Context context, String cityName, String weatherCode, String temp1, String temp2, String weatherDesp, String publishTime) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年M 月d 日", Locale.CHINA); Log.d("msg",temp1 ); SharedPreferences.Editor editor = PreferenceManager .getDefaultSharedPreferences(context).edit(); editor.putBoolean("city_selected", true); editor.putString("city_name", cityName); editor.putString("weather_code", weatherCode); editor.putString("temp1", temp1); editor.putString("temp2", temp2); editor.putString("weather_desp", weatherDesp); editor.putString("publish_time", publishTime); editor.putString("current_date", sdf.format(new Date())); editor.commit(); }}
3. 创建一个活动界面(未完)
原标题:Android 遍历全国的地区二(获取天气)
关键词:Android