.NET开发

本类阅读TOP10

·vs.net 2005中文版下载地址收藏
·NHibernate快速指南(翻译)
·【小技巧】一个判断session是否过期的小技巧
·通过Web Services上传和下载文件
·?dos下编译.net程序找不到csc.exe文件
·VB/ASP 调用 SQL Server 的存储过程
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
·Autodesk官方最新的.NET教程(一)(vb.net版)
·Duwamish深入剖析-结构篇

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
SmartPhone 2003 手机编程实战之二:自己开发一个天气预报服务

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

                       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机数据的程序

2Microsoft 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,存储我们的天气信息

 <?xml version="1.0" encoding="utf-8" ?>

<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" />

                            </xs:sequence>

                       </xs:complexType>

                   </xs:element>

              </xs:choice>

         </xs:complexType>

     </xs:element>

</xs:schema>

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

{

     /// <summary>

     /// Service1 的摘要说明。

     /// </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;

                  

         /// <summary>

         /// 设计器支持所需的方法 - 不要使用代码编辑器修改

         /// 此方法的内容。

         /// </summary>

         private void InitializeComponent()

         {

         }

 

         /// <summary>

         /// 清理所有正在使用的资源。

         /// </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();

             

                   weatherDs.WeatherDS.AddWeatherDSRow(GetWeatherRow(ref weatherDs,weatherData,cityName) );

                   return weatherDs;

             

         }

 

 

         private WeatherDataSet.WeatherDSRow GetWeatherRow(ref WeatherDataSet weatherDs,string weatherData,string cityName)

         {

              WeatherDataSet.WeatherDSRow weatherRow = weatherDs.WeatherDS.NewWeatherDSRow();

 

              weatherRow.CityName =  weatherData.Substring(weatherData.IndexOf("")+1,cityName.Length);

              weatherRow.Date1 =     DateTime.Now.ToLongDateString();

              weatherRow.Weather1 =  weatherData.Substring(weatherData.IndexOf("天气")+"天气".Length,weatherData.IndexOf("气温")-(weatherData.IndexOf("天气")+"天气".Length));

              weatherRow.Temp1 = weatherData.Substring(weatherData.IndexOf("气温")+"气温".Length,weatherData.IndexOf("风力")-(weatherData.IndexOf("气温")+"气温".Length)).Replace("-","/");

 

              weatherRow.WindPower1 = weatherData.Substring(weatherData.IndexOf("风力")+"风力".Length,weatherData.IndexOf(tommorow)-(weatherData.IndexOf("风力")+"风力".Length));

              weatherRow.Date2 =     DateTime.Today.AddDays(1).ToLongDateString();

              weatherRow.Weather2 =  weatherData.Substring(weatherData.LastIndexOf("天气")+"天气".Length,weatherData.LastIndexOf("气温")-(weatherData.LastIndexOf("天气")+"天气".Length));

              weatherRow.Temp2 = weatherData.Substring(weatherData.LastIndexOf("气温")+"气温".Length,weatherData.LastIndexOf("风力")-(weatherData.LastIndexOf("气温")+"气温".Length)).Replace("-","/");

 

              weatherRow.WindPower2 = weatherData.Substring(weatherData.LastIndexOf("风力")+"风力".Length);

 

              return weatherRow;

         }

 

         #endregion

 

         #region GetPageString 获取QQ的天气服务

         //private string xx="";

         [WebMethod(Description="天气预报")]

         public string GetPageString(string cityName)

         {

              string url=@"http://appnews.qq.com/cgi-bin/news_qq_search";

              return GetPage(url,cityName);

         }

 

         private static string GetPage(string url,string cityName)

         {

              HttpWebResponse res = null;

              string strResult = "";

 

              try

              {

                   string postData = "city=" + HttpUtility.UrlEncode(cityName,System.Text.Encoding.GetEncoding("GB2312"));

                   HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

                   req.Method = "POST";

                   req.KeepAlive = true;

                   req.ContentType = "application/x-www-form-urlencoded";

                   StringBuilder UrlEncoded = new StringBuilder();

                   byte[] SomeBytes = Encoding.ASCII.GetBytes(postData);

                   req.ContentLength = SomeBytes.Length;

                   Stream newStream = req.GetRequestStream();

                   newStream.Write(SomeBytes, 0, SomeBytes.Length);

                   newStream.Close();

                   //获得流内容     

                   res = (HttpWebResponse)req.GetResponse();

                   System.IO.Stream s=res.GetResponseStream();

                   StreamReader reader = new StreamReader(s,System.Text.Encoding.Default);

                   strResult=reader.ReadToEnd();

 

              }

              catch(Exception e)

              {

                   strResult = e.ToString();

              }

              finally

              {

                   if ( res != null )

                   {

                       res.Close();

                   }

              }

             

              strResult=strResult.Remove(0,strResult.IndexOf(""));

 

              if( cityName != "北京" )

              {

                   strResult=strResult.Remove(strResult.IndexOf("北京"),strResult.Length-strResult.IndexOf("北京"));

              }

              else

              {

                   strResult=strResult.Remove(strResult.LastIndexOf("北京"),strResult.Length-strResult.LastIndexOf("北京"));

              }

             

              strResult=strResult.Trim();

 

              while(strResult.IndexOf(@"<") != -1)

              {

                   strResult=strResult.Remove(strResult.IndexOf(@"<"),strResult.IndexOf(@">")-strResult.IndexOf(@"<")+1);

              }

 

              while(strResult.IndexOf(@" ") != -1)

              {

                   strResult=strResult.Replace(" ","");

              }

 

              string x = Encoding.UTF8.GetString(new Byte[]{10});

              string y = Encoding.UTF8.GetString(new Byte[]{9});

 

              while(strResult.IndexOf(x) != -1)

              {

                   strResult=strResult.Replace(x,"");

              }

 

              while(strResult.IndexOf(y) != -1)

              {

                   strResult=strResult.Replace(y,"");

              }

 

              return strResult;

         }

 

 

          #endregion

     }

}

记得将在Web.Config文件加入以下节点,使得WebService能被外部访问
   
<!--  WebService 获取的途径 -->

    <webServices>

        <protocols>

              <add name="HttpSoap"/>

              <add name="HttpPost"/>

              <add name="HttpGet"/>

              <add name="HttpPostLocalhost"/æ

              <add name="Documentation"/>

         </protocols>

</webServices>

三、新建智能设备应用程序项目

1、新建一个智能设备应用程序,在主面板上拖放如图2所示的几个控件,其中timer是用来检查是否数据已经下载完毕的。



图1-新建智能设备应用程序


图2-界面设计

2、将建好的WebService引用进来


图3-引用WebServiceusing

3、WeatherService智能手机上运行的程序源代码

using System;

using System.Drawing;

using System.Collections;

 System.Windows.Forms;

using System.Data;

namespace WeatherService

{