[. Net MF network development board research-03] Get Yahoo weather

Source: Internet
Author: User
Tags drawtext

 

The previous article introduced the Http Server and accessed the Http service on the Development Board through the IE browser on the PC (equivalent to the Http client. This time, we implemented the Http Client on the Network Development Board to obtain weather information on the Yahoo website and display the information on the LCD.

The Code contains two parts: first, get data through Http, and second, parse the obtained webpage in XML to obtain weather information.

The main program is simple, that is, web Service requests and screen display.

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

}

Create an Http request and obtain data. The Code is as follows:

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;

}

After the data is obtained, parse the necessary XML to extract the weather data.

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

}

}

After the data is parsed, the screen is displayed.

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

}

}

}

Ensure that the development board is connected to the Internet correctly. Note that you do not need to set up the DNS server. However, if you test the official HttpClient instance, you must set it, because the current mf lwip protocol stack does not support the default DNS), run the program, then in the Super Terminal, we can see the data we requested from the Internet (such ):

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.