ITPub博客

首页 > Linux操作系统 > Linux操作系统 > SmartPhone手机上查看QQ天气预报(转)

SmartPhone手机上查看QQ天气预报(转)

原创 Linux操作系统 作者:jcszjswkzhou 时间:2019-01-15 09:18:04 0 删除 编辑
智能手机应用程序开发是软件开发的一个新的热点,但如何才能跨入这道门楣呢?今天我通过为自己的多普达565手机添加一个天气预报程序来向大家讲解一下。我们都知道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

  下载网址:http://www.microsoft.com/windowsmobile/downloads/activesync37.mspx,里面有中文版本,或者,在手机附带的微软光盘里面有安装程序;最新版本Microsoft ActiveSync 3.8出来了,可以到摘要的页面中去找链接下载,但这个程序我还是用的老版本。

  作用:同步手机和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

  二、设计并部署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>


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-126732/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-08-29

  • 博文量
    3488
  • 访问量
    2621543