Smartphone
Mobile development -
Develop a weather forecast service
Author: He Galaxy (hxhbluestar)
Time: 2005-1-16
[Article Guide]
This article describes a program that develops a weather forecast service on a smartphone based on the SmartPhone operating system. [Introduction]
We all know that QQ has a free offer to blog quoted weather forecast service URL http://appnews.qq.com/cgi-bin/news_qq_search?city= Shanghai (Shanghai is my own city, if you want to see your city, Change to the city name in the browser), now I use this service provided by QQ, deploy it into a web service, and write programs to make my Dopod 565 smartphone can use this web service.
[text]
PC Development Environment: Windows XP SP2, Visual Studio 2003.net, .NET Framework 1.1 SP1
Operating System: Windows Mobile (TM) 2003 Second Edition, Version 4.21.1088 (Build 14235.2.0.0)
Smartphone model: Dopod 565
First, install the necessary environment
Note: The development environment you need to install .NET Mobile
The software that must be installed (the following software is Microsoft to provide free download and use) 1, Microsoft ActiveSync 3.7.1 (the latest version Microsoft ActiveSync 3.8 out, you can go to the summary page to find the link download, this I haven't tried it yet Download URL: http://www.microsoft.com/WindowsMobile/downloads/activeSync37.mspx, there is a Chinese version, or have an installer in Microsoft CDs that came with your phone; role: Synchronize your phone and PC data program
2, Microsoft SMARTPHONE 2003 SDK.msi download URL: http: //download.microsoft.com/download/e/3/1/e310bb99-2f33-4d79-bb8a-41d9cb3c79b4/Microsoft SMARTPHONE 2003 SDK.msi 3, MobileAppDevToolkit2004.exe Download address: http://download.microsoft.com/download/b/2/5/b25742c0-daa3-4a8c-988d-a947a35e68/mobileappdevtoolkit2004.exe 2, deploy QQ service into WebService
1. Build a WeatherService WebService and turn the QQ weather service to the XML WebService service, and deploy on a server with fixed IP.
2, create a new weatherdataset.xsd, store our weather information
XML Version = "1.0" encoding = "UTF-8"?>
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: sequence> xs: complexType> xs: element> xs: kice> xs: complexType> xs: element> xs: schema> 3 The source code of WeatherService is as follows #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; #ndregionnamespace WeatherService { /// /// service1's summary description. /// summary> [WebService (description = "WeatherService Weather", Namespace = "WeatherService"]]] Public Class Weather: System.Web.Services.WebService { #Region Variable PRIVATE STRING TOMMOW; #ndregion #REGION constructor Public weather () { InitializationComponent (); IF (DateTime.today.Adddays (1) .MONTH.TOSTRING (). Length == 1) { Tommorow = "0" datetime.today.addday (1) .MONTH.TOSTRING () "Month" DateTime.today.Adddays (1) .day.tostring () "Day"; } Else { Tommorow = DateTime.today.Adddays (1) .MONTH.TOSTRING () "Month" DateTime.today.Adddays (1) .day.tostring () "Day"; } } #ndregion #REGION component designer generated code / / Web service designer necessary Private icontainer Components = NULL; /// /// Designer supports the required method - do not use the code editor to modify /// This method is content. /// summary> Private vidinitiRizeComponent () { } /// /// Clean all the resources being used. /// summary> Protected Override Void Dispose (Bool Disposing) { IF (Disposing && Components! = NULL) { Components.dispose (); } Base.dispose (Disposing); } #ndregion #Region [OK] getWeatherDataSet weather forecast [WebMethod (Description = "Weather Forecast")] 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 ("Sorry, there is no weather information in this city!"); } //System.diagnostics.trace.writeline (Tommorow); //System.diagnostics.trace.writeline (WeatherData); WeatherDataSet WeatherDS = new weatherdatan (); WeatherDS.WeatherDs.AddWeatherDSRow (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 ("Weather") "Weather" .lexof ("WeatherData.indexof (WeatherData.indexof (" Weather ") " Weather ".length); WeatherData.temp1 = weatherdata.substring (WeatherData.indexof ("Temperature") "Temperature" .length, WeatherData.indexof ("Wind") - ("Water") "Temperature" "Temperature" .length)). Replace ("° C -", "° C /"); Weatherrow.windpower1 = weatherData.substring (WeatherData.indexof ("Wind") "Wind" .length, WeatherData.indexof (Tommorow) - (WeatherData.indexof ("Wind") "Wind" .length); Weatherrow.date2 = datetime.today.adddays (1) .tolongdateString (); WeatherData.SubString (WeatherData.lastIndexof (Weather ") " Weather ".length, WeatherData.lastIndexof (" WeatherData.lastIndexof ("Weather") "Weather"); WeatherRow.temp2 = weatherData.substring (WeatherData.lastIndexof ("Temperature") "Temperature" .length, WeatherData.lastIndexof ("Wind") - ("AirData.lastIndexof). Replace ("° C -", "° C /"); Weatherrow.windpower2 = weatherData.substring (WeatherData.lastIndexof ("Wind") "Wind" .length; Return WeatherRow; } #ndregion #Region getPageString Get QQ weather services // private string xx = "" [WebMethod (Description = "Weather Forecast")] 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); 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 (); // Get streaming content 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. } } Strresult = strresult.remove (0, Strresult.indexof ("●")); IF (CityName! = "Beijing") { Strresult = Strresult.remove (Strresult.indexof ("Beijing"), Strresult.Length-Strresult.indexof ("Beijing")); } Else { Strresult = Strresult.remove (Strresult.lastIndexof ("Beijing"), Strresult.Length-Strresult.lastIndexof ("Beijing"));} 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; } #ndregion } } Remember to add the following nodes in the web.config file, so that WebService can be accessed by external access to the way -> protocols> WebServices> Third, new intelligent equipment application project 1. Create a new smart device application, drag and drop on the main panel, as several controls shown in Figure 2, where timer is used to check if the data has been downloaded. Figure 1 - New Intelligent Equipment Application Figure 2 - Interface design 2, will be built by WebService Figure 3- Reference WebServiceUsing 3, the program source code running on the WeatherService smartphone; Using system.drawing; Using system.collections; System.windows.forms; Using system.data; Namespace WeatherService { /// /// Summary Description for Mainform. /// summary> Public Class Mainform: System.Windows.Forms.form { Private system.windows.Forms.comBobox ComboBox_city; private system.windows.Forms.MenuItem Menuitem1; Private system.windows.forms.label txt_weather1; Private system.windows.Forms.Label txt_date1; Private system.windows.Forms.Label txt_temp1; Private system.windows.Forms.Label txt_windpower1; Private system.windows.Forms.Label txt_temp2; Private system.windows.Forms.Label txt_windpower2; Private system.windows.Forms.Label txt_date2; Private system.windows.forms.label txt_weather2; Private system.windows.Forms.mainMenu mainmentu1; Private system.windows.Forms.MenuItem MenuItem_Menu; Private system.windows.Forms.MenuItem MenuItem_exit; Private system.windows.Forms.Label label_city; Private system.windows.forms.timer Timer1; Private system.windows.Forms.MenuItem Menuitem2; Private system.windows.Forms.MenuItem MenuItem3; Public string [] citylist = new string [] { "Beijing", "Shanghai", "Tianjin", "Shijiazhuang", "Harbin", "Shenyang", "Changchun", "Taiyuan", "Jinan", "Zhengzhou", "Tianjin", "Hohhot", "Xi'an", "Yinchuan", "Lanzhou", "Xining", "Urumqi", "Hefei", "Nanchang", "Nanjing", "Hangzhou", "Wuhan", "Changsha", "Guangzhou", "Shenzhen", "Fuzhou", "Xiamen", "Nanning", "Guilin", "Haikou", "Chongqing", "Chengdu", "Guiyang", "Kunming", "Lhasa", "Hong Kong", "Macao", "Taipei", } Public mainform () { InitializationComponent (); FOREACH (String X in CityList) { this.combobox_city.items.add (x); } this.comboBox_city.selectedIndex = 0; } /// /// Clean Up Any Resources Being Used. /// summary> Protected Override Void Dispose (BOOL Disposing) { Base.dispose (Disposing); } #Region Windows Form Designer Generated Code /// /// Required Method for Designer Support - Do Not Modify /// The contents of this method with the code editor. /// summary> Private vidinitiRizeComponent () { This.mainMenu1 = new system.windows.Forms.mainMenu (); THIS.MENUITEM1 = New System.windows.Forms.Menuitem (); THIS.MENUITEM_MENU = New System.windows.Forms.MenuItem (); THIS.MENUITEM_EXIT = New System.windows.Forms.MenuItem (); THIS.MENUITEM3 = New System.windows.Forms.MenuItem (); THIS.MENUITEM2 = New System.windows.Forms.MenuItem (); This.comBobox_city = new system.windows.forms.comboBOX (); THIS.Label_city = new system.windows.forms.label (); THIS.TXT_WEATHER1 = new system.windows.forms.label (); THIS.TXT_DATE1 = New System.windows.Forms.label (); THIS.TXT_TEMP1 = New System.windows.Forms.label (); THIS.TXT_WINDPOWER1 = new system.windows.forms.label (); THIS.TXT_TEMP2 = New System.windows.Forms.Label (); THIS.TXT_WINDPOWER2 = new system.windows.forms.label (); THIS.TXT_DATE2 = New System.windows.Forms.label (); THIS.TXT_WEATHER2 = new system.windows.forms.label (); This.timer1 = new system.windows.forms.timer (); // // mainmenu1 // THIS.MAINMENU1.MENUITEMS.ADD (this.Menuitem1); This.mainMenu1.Menuitems.Add (this.MenuItem_Menu); // // Menuitem1 // this.Menuitem1.text = "OK"; THIS.MENUITEM1.Click = new system.eventhandler (this.Menuitem1_click); // // Menuitem_Menu // THIS.MENUITEM_MENU.MENUITEMS.ADD (this.MenuItem_exit); THIS.MENUITEM_MENU.MENUITEMS.ADD (this.Menuitem3); THIS.MENUITEM_MENU.MENUITEMS.ADD (this.Menuitem2); this.Menuitem_Menu.Text = "Menu"; // // Menuitem_exit // this.Menuitem_exit.text = "Exit"; THIS.MENUITEM_EXIT.CLICK = New System.EventHandler (this.MenuItem_exit_click); // // Menuitem3 // THIS.MENUITEM3.TEXT = "-"; // // Menuitem2 // THIS.MENUITEM2.TEXT = "About"; THIS.MENUITEM2.Click = new system.eventhandler (this.Menuitem2_click); // // ComboBox_city // This.combobox_city.font = new system.drawing.font ("nina", 9f, system.drawing.fontstyle.regular; this.combobox_city.location = new system.drawing.point (52, 8); This.comboBox_city.size = new system.drawing.size (114, 22); // // label_city // THIS.Label_city.font = new system.drawing.font ("nina", 9f, system.drawing.fontstyle.bold); THIS.Label_city.location = new system.drawing.point (4, 12); THIS.Label_city.size = new system.drawing.size (52, 16); THIS.Label_city.text = "City:"; THIS.Label_city.textalign = system.drawing.contentalignment.topcenter; // // txt_weather1 // THIS.TXT_WEATHER1.FONT = New System.drawing.Font ("Nina", 9F, System.drawing.FontStyle.Regular; THIS.TXT_WEATHER1.LOCATION = new system.drawing.point (12, 50); this.txt_weather1.size = new system.drawing.size (152, 14); THIS.TXT_WEATHER1.TEXT = "Weather:"; // // txt_date1 // This.txt_date1.font = new system.drawing.font ("nina", 9f, system.drawing.fontstyle.regular; this.txt_date1.location = new system.drawing.point (12, 34); This.txt_date1.size = new system.drawing.size (152, 14); THIS.TXT_DATE1.TEXT = "Date:"; // // txt_temp1 // THIS.TXT_TEMP1.FONT = New System.drawing.Font ("Nina", 9F, System.drawing.FontStyle.Regular; this.txt_temp1.location = new system.drawing.point (12, 68); this.txt_temp1.size = new system.drawing.size (152, 14); THIS.TXT_TEMP1.TEXT = "Temperature:"; // // TXT_WINDPOWER1 // THIS.TXT_WINDPOWER1.FONT = New System.drawing.Font ("Nina", 9F, System.drawing.FontStyle.Regular; THIS.TXT_WINDPOWER1.LOCATION = New System.drawing.Point (12, 84); this.txt_windpower1.size = new system.drawing.size (152, 14); THIS.TXT_WINDPOWER1.TEXT = "Wind:"; // // TXT_TEMP2 // THIS.TXT_TEMP2.FONT = New System.drawing.Font ("Nina", 9F, System.drawing.FontStyle.Regular; This.txt_temp2.location = new system.drawing.point (12, 140); This.txt_temp2.size = new system.drawing.size (152, 14); THIS.TXT_TEMP2.TEXT = "Temperature:"; // // TXT_WINDPOWER2 // THIS.TXT_WINDPOWER2.FONT = New System.drawing.Font ("Nina", 9F, System.drawing.FontStyle.Regular; THIS.TXT_WINDPOWER2.LOCATION = New System.drawing.Point (12, 156); THIS.TXT_WINDPOWER2.SIZE = New System.drawing.size (152, 14); THIS.TXT_WINDPOWER2.TEXT = "Wind:"; // // txt_date2 // THIS.TXT_DATE2.FONT = New System.drawing.Font ("Nina", 9F, System.drawing.FontStyle.Regular; this.txt_date2.location = new system.drawing.point (12, 108); This.txt_date2.size = new system.drawing.size (152, 14); THIS.TXT_DATE2.TEXT = "Date:"; // // txt_weather2 // THIS.TXT_WEATHER2.FONT = New System.drawing.Font ("Nina", 9F, System.drawing.FontStyle.Regular); This.txt_Weather2.Location = new system.drawing.point (12, 124); THIS.TXT_WEATHER2.SIZE = New System.drawing.size (152, 14); this.txt_weather2.text = "Weather:"; // // Timer1 // this.timer1.interval = 200; This.Timer1.Tick = new system.eventhandler (this.timer1_thing); // // mainform // This.Controls.add (this.comboBox_city); This.Controls.add (this.txt_date1); This.Controls.add (this.txt_weather1); This.Controls.add (this.label_city); This.Controls.add (this.txt_temp1); This.Controls.add (this.txt_windpower1); This.Controls.add (this.txt_temp2); This.Controls.add (this.txt_windpower2); This.Controls.add (this.txt_date2); This.Controls.add (this.txt_weather2); THIS.MENU = this.mainMenu1; THIS.TEXT = "Weather Forecast"; } #ndregion /// /// The main entry point for the application. /// summary> Static void main () { Application.run (new mainform ()); } Private void menuitem_exit_click (Object Sender, System.EventArgs E) { this.dispose (); } Private void Menuitem1_Click (Object Sender, System.Eventargs E) { SearchWeather (this.comboBox_city.text); } Private string [] mystring = new string [] { "Date:", "Temperature:", "the weather: ", "Wind:" } Public void searchWeather (String CityName) { IF (this.txt_date1.text! = mystring [0]) { RESELECT (); } this.Text = "Download ..."; THIS.TIMER1.ENABED = TRUE; WeatherService.Weather WEATHER = New WeatherService.Weather (); Dataset myds = weather.getWeatherDataSet (CityName); This.txt_date1.text = mystring [0] myds.tables [0] .rows [0] ["date1"]. TOSTRING (); This.txt_temp1.text = mystring [1] myds.tables [0] .rows [0] ["TEMP1"]. TOSTRING (); THISTXT_WEATHER1.TEXT = mystring [2] myds.tables [0] .ROWS [0] ["Weather1"]. TOSTRING (); this.txt_windpower1.text = mystring [3] myds.tables [0]. Rows [0] ["Windpower1"]. TOSTRING (); This.txt_date2.text = mystring [0] myds.tables [0] .rows [0] ["date2"]. TOSTRING (); THIS.TXT_TEMP2.TEXT = mystring [1] myds.tables [0] .ROWS [0] ["Temp2"]. Tostring (); THISTXT_WEATHER2.TEXT = mystring [2] myds.tables [0] .ROWS [0] ["Weather2"]. Tostring (); THISTXT_WINDPOWER2.TEXT = mystring [3] myds.tables [0] .rows [0] ["windpower2"]. TOSTRING (); } Private vid reselectr { THIS.TXT_DATE1.TEXT = mystring [0]; THIS.TXT_TEMP1.TEXT = mystring [1]; THIS.TXT_WEATHER1.TEXT = mystring [2]; THIS.TXT_WINDPOWER1.TEXT = MyString [3]; THIS.TXT_DATE2.TEXT = mYString [0]; THIS.TXT_TEMP2.TEXT = mystring [1]; THIS.TXT_WEATHER2.TEXT = mystring [2]; THIS.TXT_WINDPOWER2.TEXT = MyString [3]; } Private void Timer1_Tick (Object Sender, System.EventArgs E) { IF (this.txt_date1.text.indexof ("Month")! = -1) { this.timer1.enabled = false; THIS.TEXT = "Weather Forecast"; } } Private void Menuitem2_click (Object Sender, System.Eventargs E) { this.Text = "Author: He Galaxy"; System.threading.Thread.sleep (3000); THIS.TEXT = "Weather Forecast"; } } } Fourth, deploy the developed program to Dopod 565 smartphone and run 1. On the "Solution WeatherService" of VS.Net2003, click Right click on the "Deployment Solution", and the dialog box appears. Figure 4 - Deployment Solution After selecting "Deploy", "Deploy" is selected, the following interface will be displayed on the phone. Figure 5, one of the mobile interface - deployment Because I installed .NET Framework1.1sp1 on my PC machine, I will prompt this interface, select "OK", continue to perform the installation deployment: Figure 6. Two of the mobile interface - Select the installation location Select the installation location to "SOTRAGE CARD", select Finish, this time the program is deployed, Using Resco Explore 2003, find the directory where the program is installed, in the / Sorage / Program Files / WeatherService / Directory, execute the program weatherService.exe, will appear as follows: Figure 7, the three mobile interface - start executing the program Select "Yes", this picture, because the program does not have systematic authentication, and it is also an embodiment of Microsoft security. Instant this program will be implemented normally, and it will not take a call! After the program execution, the following screen will appear, select the name of the city you need to query, click "OK" Figure 8, the four-program interface of the mobile phone interface The program title will change to "download ...", the program reads the WEBSERVICE deployed on the Internet via GPRS. Figure 9. Five-run interface After a few seconds, you will see the weather information data required. Figure 10, mobile phone interface six - operation results Full text