日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

【.Net MF网络开发板研究-03】获取雅虎天气(HttpClient示例)

發(fā)布時(shí)間:2024/4/14 asp.net 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【.Net MF网络开发板研究-03】获取雅虎天气(HttpClient示例) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

???? ?在上篇文章介紹了Http Server,通過PC上的IE瀏覽器(相當(dāng)于Http client)來訪問開發(fā)板上的Http服務(wù)。這次我們在網(wǎng)絡(luò)開發(fā)板上實(shí)現(xiàn)Http Client,獲取雅虎網(wǎng)站的天氣信息,并把這些信息在LCD上顯示出來。

????? 包含兩部分的代碼,一是通過Http協(xié)議獲取數(shù)據(jù),二是對獲取的網(wǎng)頁,進(jìn)行XML解析,以期獲取天氣信息。

????? 主程序很簡單,就是web服務(wù)請求和畫面顯示。

?????? public static void Main()

??????? {

??????????? try

??????????? {

??????????????? weather = new yahooWeatherRequest();

??????????????? weather.webRequest();

??????????? }

??????????? catch

??????????? {

??????????????? Debug.Print("Error!");

??????????? }

??????????? WindowsDrawing win = new WindowsDrawing();

??????????? win.Width = SystemMetrics.ScreenWidth;

??????????? win.Height = SystemMetrics.ScreenHeight;

??????????? new Program().Run(win);

??????? }

????? 創(chuàng)建Http請求,并獲取數(shù)據(jù),相關(guān)代碼如下:

??? ??private byte[] getHttpData(string url)

??????? {

??????????? HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;

??????????? request.HttpsAuthentCerts = null;

??????????? request.KeepAlive = true;

??????????? WebResponse resp = null;

??????????? Stream respStream = null;

??????????? byte[] bytData = null;

??????????? try

??????????? {

??????????????? resp = request.GetResponse();

??????????? }

??????????? catch (Exception e)

??????????? {

??????????????? Debug.Print("Exception in HttpWebRequest.GetResponse(): " + e.Message.ToString());

??????????????? return null;

??????????? }

?

??????????? if (resp != null)

??????????? {

??????????????? respStream = resp.GetResponseStream();

??????????????? int bytesRead = 0;

??????????????? int totalBytes = 0;

??????????????? respStream.ReadTimeout = 5000;

??????????????? Debug.Print("resp length= " + resp.ContentLength.ToString());

??????????????? if (resp.ContentLength!=-1)

??????????????? {

??????????????????? bytData = new byte[resp.ContentLength];

??????????????????? while (totalBytes < bytData.Length)

??????????????????? {

???????????????????????? bytesRead = respStream.Read(bytData, totalBytes, bytData.Length - totalBytes);

???????????????????????? if (bytesRead == 0)

???????????????????????? {

???????????????????????????? Debug.Print("Error: Received " + totalBytes.ToString() + " Out of " + bytData.Length.ToString());

???????????????????????????? bytData = null;

???????????????????????????? break;

???????????????????????? }

???????????????????????? totalBytes += bytesRead;

???????????????????????? Debug.Print("Bytes Read Now 0: " + bytesRead + " Total: " + totalBytes);

??????????????????? }

??????????????????? return bytData;

??????????????? }

??????????????

??????????? }

??????????? if (respStream != null) respStream.Close();

??????????? if (resp != null) resp.Close();

??????????? request = null;

??????????? return bytData;

??????? }

??????? 數(shù)據(jù)獲取后,進(jìn)行必要的XML解析,以提取天氣數(shù)據(jù)。

??????? private void parseRssPage(byte[] rssPage)

??????? {

??????????? MemoryStream mStream = new MemoryStream(rssPage);

??????????? XmlReader xReader = XmlReader.Create(mStream);

??????????? forcastArray = new ArrayList();

??????????? while (xReader.Read())

??????????? {

??????????????? if (xReader.NodeType == XmlNodeType.Element)

??????????????? {

??????????????????? switch (xReader.Name)

??????????????????? {

?????????????????? ?????case "title":

??????????????????????????? xReader.Read();

??????????????????????????? break;

??????????????????????? case "pubDate":

??????????????????????????? xReader.Read();

??????????????????????????? break;

??????????????????????? case "yweather:location":

??????????????????????????? myCity = new cityInfo(xReader.GetAttribute("city"), xReader.GetAttribute("region"), xReader.GetAttribute("country"));

??????????????????????????? break;

??????????????????????? case "yweather:condition":

??????????????????????????? today = new todayCondition(xReader.GetAttribute("text"), xReader.GetAttribute("temp"), xReader.GetAttribute("date"));

???????????????????????????? break;

??????????????????????? case "yweather:forecast":

??????????????????????? ????forcastArray.Add(new forcastCondition(xReader.GetAttribute("day"), xReader.GetAttribute("date"), xReader.GetAttribute("low"),

??????????????????????????????? xReader.GetAttribute("high"), xReader.GetAttribute("text")));

???????????????????????????? break;

??????????????????? }

??????????????? }

??????????????? else if (xReader.NodeType == XmlNodeType.CDATA)

??????????????????? parseCDATA(xReader.Value);

??????????? }

??????? }

??????? 數(shù)據(jù)解析完畢后,就進(jìn)行屏幕顯示了。

??????????? public override void OnRender(DrawingContext dc)

??????????? {

??????????????? dc.DrawRectangle(new SolidColorBrush(Colors.White), new Pen(Colors.White), 0, 0, Width, Height);

??????????????? dc.DrawLine(new Pen(Colors.Gray), 10, 46, 310, 46);?????????????? dc.DrawImage(Resources.GetBitmap(Resources.BitmapResources.yahoo_news_wea), 10, 10);

??????????????? if (Program.weather != null)

??????????????? {

??????????????????? int Y = 60;

??????????????????? if (weather.MyCity != null) dc.DrawText(weather.MyCity.ToString(), Resources.GetFont(Resources.FontResources.small), Colors.Black, 10, Y);

??????????????????? if (weather.Today != null)

??????????????????? {

??????????????????????? dc.DrawText(weather.Today.date, Resources.GetFont(Resources.FontResources.small), Colors.Black, 10, Y + 20);

??????????????????????? dc.DrawText(weather.Today.weahterDesc + "?? temperature: " + weather.Today.curTemp + "c", Resources.GetFont(Resources.FontResources.small), Colors.Black, 10, Y + 40);

??????????????????? }

??????????????????? dc.DrawText("Forcast -- this week:", Resources.GetFont(Resources.FontResources.small), Colors.Black, 10, Y + 80);

??????????????????? Y += 80;

??????????????????? if (weather.ForcastArray != null)

??????????????????? {

??????????????????????? foreach (yahooWeatherRequest.forcastCondition forcast in weather.ForcastArray)

??????????????????????? {

??????????????????????????? string info = forcast.date + " , " + forcast.day + " , " + forcast.weahterDesc + " , " + forcast.lowTemp + "c ~ " + forcast.highTemp + "c ";

??????????????????????????? Y += 20;

??????????????????????????? dc.DrawText(info, Resources.GetFont(Resources.FontResources.small), Colors.Black, 10, Y);

??????????????????????? }

??????????????????? }

??????????????? }

????? 保證開發(fā)板正確的接入互聯(lián)網(wǎng),注意設(shè)置好DNS服務(wù)器(這個(gè)事例也可以不用設(shè)置,不過如果測試官方的HttpClient事例,是一定要設(shè)置的,因?yàn)槟壳癕F的LWIP協(xié)議棧不支持默認(rèn)的DNS),運(yùn)行程序,則在超級終端中,我們可以看到我們從互聯(lián)網(wǎng)上請求的數(shù)據(jù)(如下圖):

???? ?????

?????開發(fā)板運(yùn)行后的畫面如下:

????? ?

???? 開發(fā)板最新的固件版本:V0.9.06? 下載地址:http://www.sky-walker.com.cn/MFRelease/firmware/mfv41_firmware_hy_redbull.rar

?----------------------------------------------------------------------------------------------

源碼/文檔:http://www.sky-walker.com.cn/MFRelease/Sample/YFHttpClient.rar

MF快速入門:http://blog.csdn.net/yefanqiu/article/details/5340560

MF論壇:http://space.cnblogs.com/group/MFSoft/

MF開發(fā)板:http://item.taobao.com/item.htm?id=7117999726

網(wǎng)絡(luò)開發(fā)板:http://item.taobao.com/item.htm?id=10919470266

QQ群:127465602(已滿) 146524112

總結(jié)

以上是生活随笔為你收集整理的【.Net MF网络开发板研究-03】获取雅虎天气(HttpClient示例)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。