"Original" Wm uses Google weather API to obtain weather forecasts

Source: Internet
Author: User

Recently, the project has been very busy, and many of the company's chores have come to me, so I can't concentrate on the project, simply, today I took the time to play with Google's weather forecast service. Believe in this articleArticleTo let you know how to initiate an HTTP access request (webrequest) under. Net CF and process the requested data, such as XML data and image data. Of course, it's also entertaining to share it here!

Start, we need to know, Google's Service API, through which you enter http://www.google.com/ig/api in the browser? Weather = Kunming: What is the effect? haha, the data in an XML structure is displayed on the webpage. This is the main role of today! In fact, WM usually obtains this type of data through WebService, but some information is provided through the previous HTTP method.ProgramTo obtain the weather forecast, perform the following steps:

    • Initiate an HTTP request: httpwebrequest
    • Process the data returned by this request: httpwebresponse
    • Organize the data into readable formats: Ui

Lucky ,. net CF provides related classes for HTTP requests and processing. Of course, its support for XML document operations is also very user-friendly. Before starting coding, I would like to popularize some knowledge:

---- HTTP request, see the figure!

In addition, I have attached a good article: HTTP Request Process (1)-process introduction. I hope you can learn it with patience.CodeWill involve some knowledge, haha.

Okay. Let's start coding!

 

First, use vs2008 to create a C # language-based smart device application. The simulator can use 5.0 PPC,. Net CF version, 2.0 ~~~~~

Then we design the following user interface:

Several labels are used to store weather data, and a picturebox is used to store weather icons. Two buttons: one to enable the main function and the other to exit! Haha. This interface is complete. Let's take a look at the specific business code. Double-click the get information button and add the following code:

 

Getweather

  Private     Void  Getweatheritem_click (  Object  Sender, eventargs E)
{
Int I = 0 ;
// Start extracting weather information from the XML file to lable
Xmlnodelist ggnodelist = Googleweatherapi_parser ( @" Http://www.google.com/ig/api? Weather = Kunming " ). Selectnodes ( " Xml_api_reply/weather/current_conditions " );
Lbltempc. Text = Ggnodelist. Item (I). selectsinglenode ( " Temp_c " ). Attributes [ " Data " ]. Innertext; // Obtain temperature information
Lblhumidity. Text = Ggnodelist. Item (I). selectsinglenode ( " Humidity " ). Attributes [ " Data " ]. Innertext; // Obtain humidity Information
Lblcurtweather. Text = Ggnodelist. Item (I). selectsinglenode ( " Condition " ). Attributes [ " Data " ]. Innertext; // Obtain Weather Information
Lblwind_condition.text = Ggnodelist. Item (I). selectsinglenode ( " Wind_condition " ). Attributes [ " Data " ]. Innertext; // Obtain wind direction information
// Obtain the URL of a remote image and assemble it.
String Picpath = " Http://www.google.com " + Ggnodelist. Item (I). selectsinglenode ( " Icon " ). Attributes [ " Data " ]. Innertext; // Obtain the remote image address

Picboxweather. Image = Getpicimage (picpath ); // Use the getpicimage method to convert a remote image to an image object that can be used by the. net cf image control!
}

 

The code is very easy to understand. Net CF and it is quite convenient to operate XML data. We have read the data of the selected node and assigned it to the lable control. It should be clear at a glance. I will not repeat it here. Let's talk about two points: first, the googleweatherapi_parser method; second, the getpicimage method.

First, the googleweatherapi_parser method is mainly used to initiate an httpwebrequest and respond to the request to accept XML data. The Code is as follows:

 

Googleweatherapi_parser

  ///     <Summary>  
/// Obtain the returned XML file structure from the remote address
/// </Summary>
/// <Param name = "baseurl"> Remote URL </Param>
/// <Returns> </returns>
Private Static Xmldocument googleweatherapi_parser ( String Baseurl)
{
Httpwebrequest gg_request;
Httpwebresponse gg_response = Null ;
Xmldocument gg_xmldoc = Null ;
Try
{
Gg_request = (Httpwebrequest) webrequest. Create ( String . Format (baseurl ));
Gg_request.useragent = @" Mozilla/5.0 (windows; U; Windows NT 5.1; ZH-CN; RV: 1.9.2.4) Gecko/20100413 Firefox/3.6.4 " ;
Gg_response = (Httpwebresponse) gg_request.getresponse ();
Gg_xmldoc = New Xmldocument ();
Gg_xmldoc.load (gg_response.getresponsestream ());
}
Catch (Exception ex)
{
Debug. writeline (ex. Message );
}
Gg_response.close ();
Return Gg_xmldoc;
}

 

Ha, it's easy. It is to initiate an HTTP request and add some request information to the header, such as the region information and browser version ...... After testing, if your region is in the United States, the returned information is in English. Of course, the information is not tested in other countries ......

Next, let's take a look at the getpicimage method, because in the XML data returned by Google, the weather image is a remote URL address, which cannot be directly used for picturebox. However, after investigation, you only need to make an httpwebrequest for the image address, then accept the returned stream, and then receive the stream through the image's fromstream method.

Actually ,. only the fromhbitmap method is available in the image class under. Net Cf. If you look at the parameters accepted by this method, you will know that the requested stream cannot be directly used and a conversion is required, for how to convert it, see the code:

 

Getpicimage

  ///     <Summary>  
/// Restore an image object from a remote URL
/// </Summary>
/// <Param name = "picpath"> Remote Image address </Param>
/// <Returns> </returns>
Private Image getpicimage ( String Picpath)
{
Bitmap bit;
Image imagepic;
Httpwebrequest gg_request;
Httpwebresponse gg_response = Null ;
Gg_request = (Httpwebrequest) webrequest. Create (picpath );
Gg_response = (Httpwebresponse) gg_request.getresponse ();

Bit = New Bitmap (gg_response.getresponsestream ());
Imagepic = Image. fromhbitmap (bit. gethbitmap ());

Gg_response.close ();
Return Imagepic;
}

 

Do you understand a lot now? Let's first accept the responsestream content through a bitmap object, and then use the gethbitmap method of Bitmap to get an hbitmap object, it can be used to "fill" an image object, and the filled image object can be used by picturebox. Maybe this is a relatively difficult part of this article. I would like to share it with you here.

OK. After talking about this, the code and ideas of the main functional modules of the system are all available. Next, let's take a look at the running effect, for example:

 

Well, the weather is beautiful and the humidity is just right! It's Kunming ~ Well, this is the end of today's article. Thank you!

References:

Google weather API call instructions

Httpwebrequest development under. NET Compact framework

Windows Mobile emulator (simulator) connected to the Internet through ActiveSync!

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.