论坛首页 移动开发技术论坛

Android实战--天气预报(API+JSON解析)

浏览 5717 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2015-07-08  
API

 学习安卓有一段时间了,应该提高自己的实战能力,做一些简单的Demo。下面我们介绍一下如何利用网络API实现天气预报功能,主要涉及到如何利用API获得网络数据,网络数据返回一般是JSON格式,这里又涉及到JSON的解析问题,这些都是比较基础的问题,应该予以掌握。

首先在http://apistore.baidu.com/?qq-pf-to=pcqq.c2c找到你想要的API,这里我们选择http://apistore.baidu.com/astore/serviceinfo/1798.html,网页里有关于API的介绍:

接口地址:http://apistore.baidu.com/microservice/weather

请求方法:GET

请求参数: 参数名 类型 必填 参数位置 描述 默认值
citypinyin string urlParam 城市拼音 beijing
请求示例:
http://apistore.baidu.com/microservice/weather?citypinyin=beijing
JSON返回示例:
{
errNum: 0,
errMsg: "success",
retData: {
   city: "北京", //城市
   pinyin: "beijing", //城市拼音
   citycode: "101010100",  //城市编码	
   date: "15-02-11", //日期
   time: "11:00", //发布时间
   postCode: "100000", //邮编
   longitude: 116.391, //经度
   latitude: 39.904, //维度
   altitude: "33", //海拔	
   weather: "晴",  //天气情况
   temp: "10", //气温
   l_tmp: "-4", //最低气温
   h_tmp: "10", //最高气温
   WD: "无持续风向",	 //风向
   WS: "微风(<10m/h)", //风力
   sunrise: "07:12", //日出时间
   sunset: "17:44" //日落时间
  }    
}

下面搞一下布局文件:

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:minHeight="30dp"  
  11.         android:text="天气预报"  
  12.         android:textSize="26dp" />  
  13.   
  14.     <LinearLayout  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:minHeight="30dp"  
  18.         android:orientation="horizontal" >  
  19.   
  20.         <EditText  
  21.             android:id="@+id/myedit"  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"  
  24.             android:layout_weight="1111" />  
  25.   
  26.         <Button  
  27.             android:id="@+id/searchweather"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content"  
  30.             android:layout_weight="1"  
  31.             android:text="查询天气 " />  
  32.     </LinearLayout>  
  33.   
  34.     <LinearLayout  
  35.         android:layout_width="fill_parent"  
  36.         android:layout_height="wrap_content"  
  37.         android:minHeight="30dp"  
  38.         android:orientation="horizontal" >  
  39.   
  40.         <TextView  
  41.             android:layout_width="wrap_content"  
  42.             android:layout_height="wrap_content"  
  43.             android:layout_weight="1"  
  44.             android:text="城市:" />  
  45.   
  46.         <TextView  
  47.             android:id="@+id/city"  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content"  
  50.             android:layout_weight="1" />  
  51.     </LinearLayout>  
  52.   
  53.     <LinearLayout  
  54.         android:layout_width="fill_parent"  
  55.         android:layout_height="wrap_content"  
  56.         android:minHeight="30dp"  
  57.         android:orientation="horizontal" >  
  58.   
  59.         <TextView  
  60.             android:layout_width="wrap_content"  
  61.             android:layout_height="wrap_content"  
  62.             android:layout_weight="1"  
  63.             android:text="天气:" />  
  64.   
  65.         <TextView  
  66.             android:id="@+id/weather"  
  67.             android:layout_width="wrap_content"  
  68.             android:layout_height="wrap_content"  
  69.             android:layout_weight="1" />  
  70.     </LinearLayout>  
  71.   
  72.     <LinearLayout  
  73. border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; pad
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics