Status code 0 on Google geocode API from iPhone/sim, but works fine on web (Non English characters)
Try: http://maps.googleapis.com/maps/api/geocode/json?address=Őrbottyán,Hungary&sensor=true
On an iPhone 4 & simulator,
-(void)requestFailed:(ASIHTTPRequest *)request {
NSLog(@"geocode fail code: %d",[request responseStatusCode]);
NSLog(@"geocoding failed: %@",[request responseString]);
}
2011-06-01 11:36:27.343 app[1174:307] geocode fail code: 0
2011-06-01 11:36:27.345 app[1174:307] geocoding failed: (null)
In a browser I get:
"results" : [
{
"address_components" : [
{
"long_name" : "Őrbottyán",
"short_name" : "Őrbottyán",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Pest",
"short_name" : "Pest",
"types" : [ "administrative_area_level_1", "political" ]
}开发者_运维百科,
{
"long_name" : "Hungary",
"short_name" : "HU",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Őrbottyán, Hungary",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 47.7138950,
"lng" : 19.34353090
},
"southwest" : {
"lat" : 47.63339999999999,
"lng" : 19.2051070
}
},
"location" : {
"lat" : 47.6846190,
"lng" : 19.2883260
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 47.7138950,
"lng" : 19.34353090
},
"southwest" : {
"lat" : 47.63339999999999,
"lng" : 19.2051070
}
}
},
"types" : [ "locality", "political" ]
}
], "status" : "OK" }
Other requests with standard English characters in them DO work and return correctly on the device.
At some point in your code you're probably using the NSURL
class, and:
The NSURL class will fail to create a new NSURL object if the path being passed is not well-formed—the path must comply with RFC 2396. Examples of cases that will not succeed are strings containing space characters and high-bit characters. Should creating an NSURL object fail, the creation methods return nil, which you must be prepared to handle.
You need to convert the high-bit characters in "Őrbottyán" into percent escapes, using [NSString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
, before passing the string to NSURL
. Modern browsers will silently make this conversion for you when you put a non-compliant string into the URL field, but in code you have to do it explicitly.
Edit to include quantumpotato's findings below: Google Maps will do the right thing if "Őrbottyán" is converted to "Orbottyan" (a "lossy" conversion to ASCII encoding), and that conversion can be performed with a round-trip through NSData
:
NSData *data = [urlString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:dataString];
[dataString release];
I suspect "lossy conversion to ASCII" may not work with all websites, but it's been tested and verified with Google Maps, so there you have it. :-)
精彩评论