0

I tried to retrieve XML Google weather API from this URL:

http://www.google.com/ig/api?weather=,,,24467280,39606640&hl=ar

It is in Arabic language.

I used: NSURLConnection to retrieve this data and then parsed it by NSXMLParser.

The received data is not correctly encoded.I tried to put it in XML file after imported it and I found data like this:

<?xml version="1.0"?><xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information><city data=""/><postal_code data=""/><latitude_e6 data="21429730"/><longitude_e6 data="39828370"/><forecast_date data="2012-03-14"/><current_date_time data="2012-03-14 17:00:00 +0000"/><unit_system data="SI"/></forecast_information><current_conditions><condition data="’«›Ú"/><temp_f data="97"/><temp_c data="36"/><humidity data="«·—ÿÊ»…: &#x662;&#x662;&#x66A;"/><icon data="/ig/images/weather/sunny.gif"/><wind_condition data="«·—Ì«Õ: Éʻ »”—⁄… 8 fl„/”«⁄…"/></current_conditions><forecast_conditions><day_of_week data="«·√—»⁄«¡"/><low data="23"/><high data="37"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="„‘„” ›Ì √€·» «·√Êfi« "/></forecast_conditions><forecast_conditions><day_of_week data="«·Œ„Ì”"/><low data="20"/><high data="35"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="„‘„” ›Ì √€·» «·√Êfi« "/></forecast_conditions><forecast_conditions><day_of_week data="«·Ã„⁄…"/><low data="18"/><high data="33"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="„‘„” ›Ì √€·» «·√Êfi« "/></forecast_conditions><forecast_conditions><day_of_week data="«·”» "/><low data="19"/><high data="33"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="„‘„” ›Ì √€·» «·√Êfi« "/></forecast_conditions></weather></xml_api_reply>

Of course, I got error from NSXMLParser:

Error Domain=NSXMLParserErrorDomain Code=9 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 9.)"

That means the xml data is corrupted.

If I change the link from Google to be:

http://www.google.com/ig/api?weather=,,,21429730,39828370&hl=en

(notice english language now), all things works correctly..

I tried to encode the retrieved Arabic XML data by:

NSString * encodedString = [[NSString alloc] initWithBytes:recievedData.bytes length:recievedData.length encoding:NSStringEncodingConversionAllowLossy];

and then I putted the content in the file by:

[[NSFileManager defaultManager] createFileAtPath:filePath contents:[encodedString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

the content now is better but it is still wrong, the content now :

<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information><city data=""/><postal_code data=""/><latitude_e6 data="21429730"/><longitude_e6 data="39828370"/><forecast_date data="2012-03-14"/><current_date_time data="2012-03-14 17:00:00 +0000"/><unit_system data="SI"/></forecast_information><current_conditions><condition data="ÕÇÝò"/><temp_f data="97"/><temp_c data="36"/><humidity data="ÇáÑØæÈÉ: &#x662;&#x662;&#x66A;"/><icon data="/ig/images/weather/sunny.gif"/><wind_condition data="ÇáÑíÇÍ: ÌäæÈ ÈÓÑÚÉ 8 ßã/ÓÇÚÉ"/></current_conditions><forecast_conditions><day_of_week data="ÇáÃÑÈÚÇÁ"/><low data="23"/><high data="37"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="ãÔãÓ Ýí ÃÛáÈ ÇáÃæÞÇÊ"/></forecast_conditions><forecast_conditions><day_of_week data="ÇáÎãíÓ"/><low data="20"/><high data="35"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="ãÔãÓ Ýí ÃÛáÈ ÇáÃæÞÇÊ"/></forecast_conditions><forecast_conditions><day_of_week data="ÇáÌãÚÉ"/><low data="18"/><high data="33"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="ãÔãÓ Ýí ÃÛáÈ ÇáÃæÞÇÊ"/></forecast_conditions><forecast_conditions><day_of_week data="ÇáÓÈÊ"/><low data="19"/><high data="33"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="ãÔãÓ Ýí ÃÛáÈ ÇáÃæÞÇÊ"/></forecast_conditions></weather></xml_api_reply>

The NSXMLParser doesn't give me error now but the text results in not readable.

Sorry for this large question.

Can any one help me?

Sachin D
  • 1,370
  • 7
  • 29
  • 42
yasserislam
  • 154
  • 1
  • 2
  • 11
  • **The Google weather API was shut down in 2012** -> http://stackoverflow.com/questions/12145820/google-weather-api-gone/35943521 – John Slegers Mar 11 '16 at 15:58

2 Answers2

0

I have used: ASIHTTPRequest libraray

It received the Arabic data with good encoding, they mentioned in documentation:

ASIHTTPRequest will attempt to read the text encoding of the received data from the Content-Type header. If it finds a text encoding, it will set responseEncoding to the appropriate NSStringEncoding. If it does not find a text encoding in the header, it will use the value of defaultResponseEncoding (this defaults to NSISOLatin1StringEncoding).

When you call [request responseString], ASIHTTPRequest will attempt to create a string from the data it received, using responseEncoding as the source encoding.

I used [request responseString] to store the received text response to temp file and then I have read the data from the file by NSXMLParser, but in fact; I could use the feature in the ASIHTTPRequest called: "Downloading the response directly to a file" instead of what I did..

By the way, my code is working perfectly now...

yasserislam
  • 154
  • 1
  • 2
  • 11
  • Sorry, we don't allow signatures/taglines, as is detailed in our [faq#signatures]. –  Dec 24 '12 at 21:22
0

Add [oe=char_set] parameter to google url as follow:

`http://www.google.com/ig/api?weather=Cairo,Egypt&hl=ar&oe=utf-8`
ciwmcit
  • 157
  • 1
  • 2