通过Web Service获取天气预报并朗读
生活随笔
收集整理的這篇文章主要介紹了
通过Web Service获取天气预报并朗读
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
環境:Visual Studio 2010 C# 1. 新建項目-->Windows窗體應用程序。輸入解決方案名,假定為TTS。 2. 在界面設計里拉一個textBox和一個Button。textBox1用來裝文本信息,點擊button1觸發獲取天氣預報事件并朗讀。 3. 在“解決方案資源管理器”中,在解決方案TTS上右鍵-->添加服務引用,彈出如下界面:
點“高級”,出現如下界面:
再點“添加Web引用”,彈出如下界面: 在地址欄上輸入http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
--------------------------------------分割線 start---------------------------------------------------- 【題外話】 這是中國提供的天氣預報的Web服務。在該站點的主頁http://www.webxml.com.cn/zh_cn/web_services.aspx上,還能找到其它諸如查詢手機號碼歸屬地、航班時刻表、匯率、英漢翻譯、QQ是否在線、驗證碼圖片生成等Web服務。 另外介紹幾個國外提供的Web Service站點(沒試過,不知能不能用): ·專門關于地圖的Web服務: http://www.opengeospatial.org/standards/wms ·一個很豐富的Web服務站點: http://www.programmableweb.com/apis/directory/1?sort=category --------------------------------------分割線 end---------------------------------------------------- 注意1:到現在的窗口標題已經為“添加Web引用”了。最開始是“添加服務引用”。這兩種是不一樣的。雖然在第一步的時候,在地址欄里輸入以上地址,前往,也能查找到這個Web服務,但是添加到項目之后,后面用的時候就有問題,不能找到需要的類和方法(具體見后邊代碼)。也可能是我不會用“服務引用”。總之我只知道怎么用“Web引用”。 注意2:留意上圖中的“Web引用名”,相當于添加的Web引用的命名空間。可以自己改。 點“添加引用”后,這個天氣預報的Web Service就添加到我們的項目里了,可在“解決方案資源管理器”里看到,如圖:
4. 接下來就是怎么使用weather web service了。 為button1添加響應函數button1_Click。具體見代碼: private void button1_Click(object sender, EventArgs e){ // get weatherstring city = "武漢";TTS.cn.com.webxml.www.WeatherWebService wws = new cn.com.webxml.www.WeatherWebService();string[] wwsArray = wws.getWeatherbyCityName(city);this.textBox1.Text = wwsArray[0] + " " + wwsArray[1] + " " + wwsArray[5] + " " + wwsArray[6] + "。" + Environment.NewLine+ "\r\n" + wwsArray[10] + "。"+ Environment.NewLine + wwsArray[11]; } 從Web Service獲取到的信息放到字符串數組wwsArray[]里,我選擇了一些字符串放到textBox1里,wwsArray[0]表示“湖北”,wwsArray[1]表示“武漢”。你也可以把整個字符串數組放進textBox1,不過注意有些信息是城市的圖片名,好像wwsArray[2]就是。 5. 添加朗讀天氣預報的功能。 首先在C盤下查找sapi.dll,我的如下圖,在兩個目錄下都有這個dll:
記住這個文件的目錄,隨便用那個都行,兩個是一樣的。 然后在Visual Studio 2010中,在“解決方案資源管理器”中項目名TTS上右鍵-->添加引用,彈出如下圖: 在瀏覽選項卡中選中我們之前在C盤里找到的sapi.dll,點確定。就添加進去了。可在項目中看到,如下圖: 這貨叫“SpeechLib”。 6. 在代碼中使用SpeechLib庫。 最終的完整代碼如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using SpeechLib;namespace TTS {public partial class Form1 : Form{public Form1(){InitializeComponent();} private void button1_Click(object sender, EventArgs e){ // speak weathertry{SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;SpVoice voice = new SpVoice();if (this.textBox1.Text.Trim() == string.Empty){// get weatherstring city = "武漢";TTS.cn.com.webxml.www.WeatherWebService wws = new cn.com.webxml.www.WeatherWebService();string[] wwsArray = wws.getWeatherbyCityName(city); this.textBox1.Text = wwsArray[0] + " " + wwsArray[1] + " " + wwsArray[5] + " " + wwsArray[6] + "。" + Environment.NewLine+ "\r\n" + wwsArray[10] + "。"+ Environment.NewLine + wwsArray[11]; voice.Speak(this.textBox1.Text, spFlags);}else{voice.Speak(this.textBox1.Text, spFlags);}}catch (Exception err){MessageBox.Show(err.Message);}} } }
出現文本信息的同時能夠聽見一個女聲在朗讀,斷句組詞什么的還算智能。
點“高級”,出現如下界面:
再點“添加Web引用”,彈出如下界面: 在地址欄上輸入http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
--------------------------------------分割線 start---------------------------------------------------- 【題外話】 這是中國提供的天氣預報的Web服務。在該站點的主頁http://www.webxml.com.cn/zh_cn/web_services.aspx上,還能找到其它諸如查詢手機號碼歸屬地、航班時刻表、匯率、英漢翻譯、QQ是否在線、驗證碼圖片生成等Web服務。 另外介紹幾個國外提供的Web Service站點(沒試過,不知能不能用): ·專門關于地圖的Web服務: http://www.opengeospatial.org/standards/wms ·一個很豐富的Web服務站點: http://www.programmableweb.com/apis/directory/1?sort=category --------------------------------------分割線 end---------------------------------------------------- 注意1:到現在的窗口標題已經為“添加Web引用”了。最開始是“添加服務引用”。這兩種是不一樣的。雖然在第一步的時候,在地址欄里輸入以上地址,前往,也能查找到這個Web服務,但是添加到項目之后,后面用的時候就有問題,不能找到需要的類和方法(具體見后邊代碼)。也可能是我不會用“服務引用”。總之我只知道怎么用“Web引用”。 注意2:留意上圖中的“Web引用名”,相當于添加的Web引用的命名空間。可以自己改。 點“添加引用”后,這個天氣預報的Web Service就添加到我們的項目里了,可在“解決方案資源管理器”里看到,如圖:
4. 接下來就是怎么使用weather web service了。 為button1添加響應函數button1_Click。具體見代碼: private void button1_Click(object sender, EventArgs e){ // get weatherstring city = "武漢";TTS.cn.com.webxml.www.WeatherWebService wws = new cn.com.webxml.www.WeatherWebService();string[] wwsArray = wws.getWeatherbyCityName(city);this.textBox1.Text = wwsArray[0] + " " + wwsArray[1] + " " + wwsArray[5] + " " + wwsArray[6] + "。" + Environment.NewLine+ "\r\n" + wwsArray[10] + "。"+ Environment.NewLine + wwsArray[11]; } 從Web Service獲取到的信息放到字符串數組wwsArray[]里,我選擇了一些字符串放到textBox1里,wwsArray[0]表示“湖北”,wwsArray[1]表示“武漢”。你也可以把整個字符串數組放進textBox1,不過注意有些信息是城市的圖片名,好像wwsArray[2]就是。 5. 添加朗讀天氣預報的功能。 首先在C盤下查找sapi.dll,我的如下圖,在兩個目錄下都有這個dll:
記住這個文件的目錄,隨便用那個都行,兩個是一樣的。 然后在Visual Studio 2010中,在“解決方案資源管理器”中項目名TTS上右鍵-->添加引用,彈出如下圖: 在瀏覽選項卡中選中我們之前在C盤里找到的sapi.dll,點確定。就添加進去了。可在項目中看到,如下圖: 這貨叫“SpeechLib”。 6. 在代碼中使用SpeechLib庫。 最終的完整代碼如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using SpeechLib;namespace TTS {public partial class Form1 : Form{public Form1(){InitializeComponent();} private void button1_Click(object sender, EventArgs e){ // speak weathertry{SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;SpVoice voice = new SpVoice();if (this.textBox1.Text.Trim() == string.Empty){// get weatherstring city = "武漢";TTS.cn.com.webxml.www.WeatherWebService wws = new cn.com.webxml.www.WeatherWebService();string[] wwsArray = wws.getWeatherbyCityName(city); this.textBox1.Text = wwsArray[0] + " " + wwsArray[1] + " " + wwsArray[5] + " " + wwsArray[6] + "。" + Environment.NewLine+ "\r\n" + wwsArray[10] + "。"+ Environment.NewLine + wwsArray[11]; voice.Speak(this.textBox1.Text, spFlags);}else{voice.Speak(this.textBox1.Text, spFlags);}}catch (Exception err){MessageBox.Show(err.Message);}} } }
?
代運行效果截圖:出現文本信息的同時能夠聽見一個女聲在朗讀,斷句組詞什么的還算智能。
轉載于:https://www.cnblogs.com/duanguyuan/archive/2013/06/09/3130142.html
總結
以上是生活随笔為你收集整理的通过Web Service获取天气预报并朗读的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决ie6、7 margin-botto
- 下一篇: vmware虚拟机环境里用linux和x