Smartphone ** 移动开发— ** ** 自己开发一个天气预报服务 **
作者:贺星河(hxhbluestar)
时间:2005-1-16
** [文章导读] **
本文介绍了在基于Smartphone操作系统的智能手机上开发一个天气预报服务的程序。
我们都知道QQ有一个免费提供给Blog引用的天气预报服务网址 http://appnews.qq.com/cgi-bin/news_qq_search?city =上海(上海是我自己所在城市,如果想看自己的城市,在浏览器中改成城市名称即可),现在我使用QQ提供的这个服务,将其包装部署为一个Web服务,并编写程序使得我的多普达565智能手机能使用这个Web服务。
** [正文] **
PC开发环境:Windows XP SP2,Visual Studio 2003.NET,.NET Framework 1.1 SP1
操作系统:Windows Mobile(TM) 2003第二版,版本4.21.1088(Build 14235.2.0.0)
智能手机型号:多普达565
一、安装必备的环境
注意:必须安装.NET Mobile所需要的开发环境
必须安装的软件 ( 如下软件都是微软提供免费下载和使用的 )
** 1 ** ** 、 ** ** Microsoft ActiveSync 3.7.1 **
( 最新版本 Microsoft ActiveSync 3.8 出来了,可以到摘要的页面中去找链接下载,这个我还没有试过 )
下载网址: http://www.microsoft.com/windowsmobile/downloads/activesync37.mspx ,里面有中文版本,或者,在手机附带的微软光盘里面有安装程序;
作用:同步手机和 PC 机数据的程序
** 2 ** ** 、 ** ** Microsoft SMARTPHONE 2003 SDK.msi **
下载网址:
http://download.microsoft.com/download/e/3/1/e310bb99-2f33-4d79-bb8a-41d9cb3c79b4/Microsoft SMARTPHONE 2003 SDK.msi
** 3 ** ** 、 ** ** MobileAppDevToolkit2004.exe **
下载地址: http://download.microsoft.com/download/b/2/5/b25742c0-daa3-4a8c-988d-a947a35e0a68/MobileAppDevToolkit2004.exe
二、将QQ的服务部署成WebService
1、建立一个名为WeatherService的WebService,并将QQ的天气服务转为XML WebService服务,部署在一台具有固定IP的服务器上。
2、新建一个WeatherDataSet.XSD,存储我们的天气信息
< xs:schema id ="WeatherDataSet" targetNamespace ="Ezhi.Services.WeatherService" elementFormDefault ="qualified"
attributeFormDefault ="qualified" xmlns ="Ezhi.Services.WeatherService" xmlns : mstns ="Ezhi.Services.WeatherService"
xmlns : xs ="http://www.w3.org/2001/XMLSchema" xmlns : msdata ="urn:schemas-microsoft-com:xml-msdata">
< xs:element name ="WeatherDataSet" msdata : IsDataSet ="true">
< xs:complexType >
< xs:choice maxOccurs ="unbounded">
< xs:element name ="WeatherDS">
< xs:complexType >
< xs:sequence >
< xs:element name ="CityName" type ="xs:string" minOccurs ="0" />
< xs:element name ="Date1" type ="xs:string" minOccurs ="0" />
< xs:element name ="Weather1" type ="xs:string" minOccurs ="0" />
< xs:element name ="Temp1" type ="xs:string" minOccurs ="0" />
< xs:element name ="WindPower1" type ="xs:string" minOccurs ="0" />
< xs:element name ="Date2" type ="xs:string" minOccurs ="0" />
< xs:element name ="Weather2" type ="xs:string" minOccurs ="0" />
< xs:element name ="Temp2" type ="xs:string" minOccurs ="0" />
< xs:element name ="WindPower2" type ="xs:string" minOccurs ="0" />
**3 WeatherService的源代码如下
**
#region Using directives
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Net;
using System.Text;
#endregion
namespace WeatherService
{
///
1<summary>
2
3/// Service1 的摘要说明。
4
5/// </summary>
[WebService(Description= "WeatherService 天气 Service" ,Namespace= "WeatherService" )]
public class Weather : System.Web.Services.WebService
{
#region Variable
private string tommorow;
#endregion
#region 构造函数
public Weather()
{
InitializeComponent();
if (DateTime.Today.AddDays( 1 ).Month.ToString().Length == 1 )
{
tommorow= "0" +DateTime.Today.AddDays( 1 ).Month.ToString()+ " 月 " +
DateTime.Today.AddDays( 1 ).Day.ToString()+ " 日 " ;
}
else
{
tommorow= DateTime.Today.AddDays( 1 ).Month.ToString()+ " 月 " +
DateTime.Today.AddDays( 1 ).Day.ToString()+ " 日 " ;
}
}
#endregion
#region 组件设计器生成的代码
//Web 服务设计器所必需的
private IContainer components = null ;
///
1<summary>
2
3/// 设计器支持所需的方法 \- 不要使用代码编辑器修改
4
5/// 此方法的内容。
6
7/// </summary>
private void InitializeComponent()
{
}
///
1<summary>
2
3/// 清理所有正在使用的资源。
4
5/// </summary>
protected override void Dispose( bool disposing )
{
if (disposing && components != null )
{
components.Dispose();
}
base .Dispose(disposing);
}
#endregion
#region [OK] GetWeatherDataSet 天气预报
[WebMethod(Description= " 天气预报 " )]
public DataSet GetWeatherDataSet( string cityName)
{
string url= @"http://appnews.qq.com/cgi-bin/news_qq_search" ;
string weatherData= "" ;
try
{
weatherData = GetPage(url,cityName).Replace( " " , "" ).Trim();
}
catch (Exception)
{
throw new Exception( " 对不起,没有这个城市的天气信息! " );
}
//System.Diagnostics.Trace.WriteLine( tommorow );
//System.Diagnostics.Trace.WriteLine( weatherData );
WeatherDataSet weatherDs = new WeatherDataSet();
<SP