0

我正在使用带有 KML 查询的 Google 地图。但我的查询字符串是“日语”字符串“マクドナルド”我正在使用http://maps.google.co.jp。请求数据时,我得到“0”字节。但是当我在浏览器中输入相同的查询时,它会下载一个 KML 文件。我的代码如下:

query = [NSString stringWithFormat:@"http://maps.google.co.jp/maps?&near=%f,%f&q=マクドナルド&output=kml&num=%d", lat,lon, num];

NSURL* url = [NSURL URLWithString:query]; NSURLRequest* request = [NSURLRequest requestWithURL:url]; NSLog(@"查询 URL = %@", url);

NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] autorelease];
NSData *myData = [NSURLConnection sendSynchronousRequest:request returningResponse: &response error: nil ];
NSInteger errorcode = [response statusCode];

我收到 0 个字节的“myData”。为什么?

4

1 回答 1

0

您需要对日文文本进行 URLencode。浏览器通常会自动为您编码文本,但 NSURL 不会。

尝试类似的东西

NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
    NULL,
    (CFStringRef)unencodedString,
    NULL,
    (CFStringRef)@"!*'();:@&=+$,/?%#[]",
    kCFStringEncodingUTF8 );
于 2010-05-21T03:08:31.300 回答