酷欧天气开发笔记3:显示天气信息

首先来创建布局文件,先思考布局文件中需要放置哪些控件,这就要由服务器返回的天气数据来决定。

{"weatherinfo":
{"city":"昆山","cityid":"101190404","temp1":"21℃","temp2":"9℃",
"weather":"多云转小雨","img1":"d1.gif","img2":"n7.gif","ptime":"11:00"}
}
cityid 是用户无需知晓的,img1 img2 我们不准备使用,需要显示的  城市名、温度范围、天气信息描述、发布时间

新建 weather_layout.xml
<pre name="code" class="html"><LinearLayout xmlns:android="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" >
<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" />
</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>
 
  Utility 类中添加几个方法,用于解析和处理服务返回的  JSON 数据 
public class Utility {
……
/**
* 解析服务器返回的JSON数据,并将解析出的数据存储到本地。
*/
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");
<span style="font-size: 14.6667px; font-family: SimSun;">String publishTime = weatherInfo.getString("ptime");</span>
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);
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();
}
}

handleWeatherResponse()方法用于将 JSON 格式的天气信息全部解析出来,saveWeatherInfo()方法用于将这些数据都存储到 SharedPreferences 文件中。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值