酷欧天气开发笔记3:显示天气信息
来源:互联网 发布:衣服淘宝店铺怎么描述 编辑:程序博客网 时间:2024/04/29 09:13
首先来创建布局文件,先思考布局文件中需要放置哪些控件,这就要由服务器返回的天气数据来决定。
{"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" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:background="#484E61" ><TextViewandroid: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><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#27A5F9" ><TextViewandroid: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" /><LinearLayoutandroid:id="@+id/weather_info_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:orientation="vertical" ><TextViewandroid:id="@+id/current_date"android:layout_width="wrap_content"android:layout_height="40dp"android:gravity="center"android:textColor="#FFF"android:textSize="18sp" /><TextViewandroid: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" /><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="60dp"android:layout_gravity="center_horizontal"android:orientation="horizontal" ><TextViewandroid:id="@+id/temp1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:textColor="#FFF"android:textSize="40sp" /><TextViewandroid: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" /><TextViewandroid: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, StringpublishTime) {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
- 酷欧天气开发笔记3:显示天气信息
- 显示天气信息总结
- 《第一行代码》之开发欧酷天气学习笔记
- 天气开发3——第二行代码(酷欧天气)
- 天气信息
- 天气信息
- 酷欧天气app开发要点
- 一个shell脚本显示当地天气信息
- 酷欧天气实战
- 酷欧天气
- 酷欧天气bug
- 天气
- 天气
- 天气
- 天气
- 天气
- 天气
- 天气
- 如何在Java IDE中使用selenium
- 1007. To and Fro
- win7 删除右下角的win10升级提示
- 类似滑动苹果解锁Text
- 灯光探测器 Light Probes
- 酷欧天气开发笔记3:显示天气信息
- Python IDLE reload(sys)后print无法正常执行命令的原因
- 虚拟地址的映射建立
- 交通序列号 简单DP
- text-align:center 和margin:0 auto的区别
- iOS进阶5--使用Jenkins搭建持续集成打包平台
- 开始我的代码博客
- 【解决】UART问题
- 流式布局的实现