Windows Phone 7 Chinese weather forecast Application

Source: Internet
Author: User

There are still too few WP7 applications and fewer Chinese applications. Although there is a weather forecast application, I feel that it is not easy to use. So I came up with my own idea.

It proves a saying from a group of friends: programmers often use other people's programs and think it is not good here. They will not find the water very deep until they want to do one.

 

The whole process has been a few twists and turns. I first searched the internet and found the Windows Phone 7 network programming weather forecast application. When I saw XML parsing, I knew it was hard to go back, because I had no fruit in learning XML at school. So this idea has been in my OneNote for a long time. Recently, I have to parse XML to understand it, but it is not that difficult. So I started to do it manually. I encountered network files that could not be directly called, and XML files were read out with garbled characters. I found a lot of materials and documents, and now I have finally implemented them.

 

 

Next let's talk about the programming process:

 

I used the LINQ to XML to parse the XML file, and when loading the XML, the first file path I directly filled in the http://www.google.com/ig/api? Weather = Guangzhou. The error message is that you cannot directly enter the web path. After looking for the information, I did not understand anything related to httpwebrequest. I used WebClient to obtain network file resources. Here, we refer to writing network resources to WP7 to isolate buckets.

string url = "http://www.google.com/ig/api?weather=guangzhou&hl=zh-cn";//&hl=zh-cn
WebClient wc = new WebClient ();
Uri uri = new Uri (url);

wc.OpenReadAsync (uri, "api.xml"); // The first method reads network resources and opens a readable stream to the specified resource
wc.OpenReadCompleted + = new OpenReadCompletedEventHandler (wc_OpenReadCompleted); // Event triggered after reading is completed

wc.DownloadStringAsync (uri, "api.xml"); // The second method reads network resources and downloads the specified string to the specified resource
wc.DownloadStringCompleted + = new DownloadStringCompletedEventHandler (wc_DownloadStringCompleted); // Event triggered after reading is completed
I do n’t understand the mechanism of triggering events after reading is complete, and I have been around for a long time. I wrote the assignment operation of the weather object in the event of reading completion, and the statement displayed on the TextBlock assignment display on the program page was written after the statement of obtaining network resources. The actual execution order is that there is no direct execution after the statement of obtaining resources is executed Jump to the statement in the read completion event (so the value of the weather object is still empty), directly execute the subsequent TextBlock assignment statement, and finally jump to the assignment of the weather object in the read completion event. As a result, the weather displayed by the program is empty. This incident has to be understood in detail.

 

After solving this problem, the program can run smoothly and update the data from the network.

 

But then the question came: visit the http://www.google.com/ig/api?weather=guangzhou xml file returned on the computer is Chinese, the author of the weather forecast application for Windows Phone 7 network programming also mentioned The xml file returned by accessing this interface in WP7 is in English, and you can see that the highest temperature and the lowest temperature are in Fahrenheit temperature units. Some people mentioned in the comment that after adding & hl = zh-cn, it returned to Chinese. After I modified the corresponding address in the program, the program became like this:

The Chinese part is all garbled! ! ! I searched the Internet and found the problem

 

The returned XML header, <? Xml version = "1.0"?>, Lacks an encoding declaration like encoding = UTF-8 compared to the standard XML header. So it is suspected that it is because of this that the SAX or DOM parser treats character encodings that are not UTF-8 as UTF-8, which leads to garbled characters and exceptions. A Google search confirmed that when using hl = zh-cn, the GBK-encoded XML was returned, and many PHP codes that used this API did GBK-> UTF-8 conversion processing.

The problem here is actually quite simple. Since GBK encoding SAX and DOM are handled by UTF-8 by default, and we can't change GOOGLE's Servlet to return an XML with encoding = GBK in the XML header. So we only have two options, either to convert the returned XML from GBK encoding to UTF-8, or to let the SAX and DOM parsers treat the XML as GBK.

 

Although it is a different platform, there are some similarities ~ Although the problem is found, the solution is not so simple. After searching for a long time, wp7 does not support GBK encoding. I found several transcoding methods and I ca n’t run it. , MSDN said only support utf8 and utf16. I ’m going to give up. I ’m already considering the weather forecast and the few data, and write a Chinese display to judge whether it is sunny and cloudy. At this time, the friends in the group gave a class that can enable Silverlight to support GB2312 Chinese encoding! After using it, I can finally see the contents of the string object in Chinese in VS during the debugging process.

 

Finally, when the string object containing Chinese xml information was passed to the xml parsing method, it reported ".", Hexadecimal value 0x00, is an invalid character. Error, it seems that the GBK support class is not perfect. So continue to find a solution, because the string contains invalid characters, it is already out of luck.

I found several ways to remove Chinese and invalid characters together, and finally found a C # method to remove xml invalid characters:

public string CleanInvalidXmlChars (string text)
{
    string re = @ "[^ \ x0D \ x20- \ xD7FF \ xE000- \ xFFFD \ x10000-x10FFFF \ u4e00- \ u9fa5]";
    return System.Text.RegularExpressions.Regex.Replace (text, re, "");
}
Finally, Chinese xml data was successfully passed in, and the program runs normally!

 

 

In addition, during the debugging process, occasionally, there will be occasional xml parsing errors. The xml parsing method has been written and it should not be wrong. After investigation, it was found to be a problem with Google. The following xml was obtained at the same time. The data of the two different cities, the Guangzhou xml file is all normal, the data attribute of conditong in the boxed content in the Guiyang xml file is missing, and the data attribute of the corresponding icon is also missing, so the parsing error. This problem appears from time to time, and now only realizes Chinese reading. In the future, you need to add the corresponding error detection code to the program, otherwise the program that parses the error will automatically exit.

 

This time writing a program has a profound impact on me, but a seemingly simple application has caused so many problems, the water is deep and the water is deep. . . But in the end it was a success and I was very happy. It also strengthened my self-confidence. In the event of difficulties, I need to do my own investigation and study. Whether it can solve the problem or not, I will definitely gain something.

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.