gpt4 book ai didi

iphone - 来自 iPhone/sim 的 Google 地理编码 API 上的状态代码 0,但在网络上运行良好(非英文字符)

转载 作者:行者123 更新时间:2023-12-03 20:23:21 25 4
gpt4 key购买 nike

尝试:http://maps.googleapis.com/maps/api/geocode/json?address =Őrbottyán,匈牙利&sensor=true

在 iPhone 4 和模拟器上,

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

在浏览器中我得到:

   "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" ]
}

], “状态”:“确定” }

其他包含标准英文字符的请求确实可以工作并在设备上正确返回。

最佳答案

在代码中的某个时刻,您可能会使用 NSURL 类,并且:

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.

在将字符串传递给 NSURL 之前,您需要使用 [NSString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 将“Őrbottyán”中的高位字符转换为百分比转义。当您将不兼容的字符串放入 URL 字段时,现代浏览器会默默地为您进行此转换,但在代码中您必须显式执行此转换。

<小时/>

编辑以包含下面的Quantumpotato的发现:如果“Őrbottyán”被转换为“Orbottyan”(到ASCII编码的“有损”转换),Google map 将会做正确的事情,并且该转换可以是通过 NSData 来回执行:

NSData *data = [urlString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:dataString];
[dataString release];

我怀疑“有损转换为 ASCII”可能不适用于所有网站,但它已经通过 Google map 进行了测试和验证,所以您已经知道了。 :-)

关于iphone - 来自 iPhone/sim 的 Google 地理编码 API 上的状态代码 0,但在网络上运行良好(非英文字符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6204718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com