【问题标题】:Unsupported URL on ios 8ios 8 上不支持的 URL
【发布时间】:2015-05-07 21:26:48
【问题描述】:

我正在尝试使用以下代码从 google 地方获取 json:

NSString *query = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%i&types=%@&sensor=true&key=%@", center.latitude, center.longitude, rad, types, kGOOGLE_API_KEY];
NSLog(@"%@",query);
NSURL *googleRequestURL=[NSURL URLWithString:query];
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:googleRequestURL] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (error) {
        NSLog(@"Error fetching data: %@",[error description]);
    } else {
        //To-do
    }
}];

生成的网址是:https://maps.googleapis.com/maps/api/place/search/json?location=37.337566,-122.041202&radius=1000&types=accounting|bowling_alley|doctor&sensor=true&key=MY_KEY

(我的密钥由于某些原因被省略了)

在我的笔记本电脑浏览器上运行良好,但返回错误:

Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x7fe47bc138f0 {NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x7fe47be9dbe0 "unsupported URL"}

我尝试使用http而不是https(在浏览器中它返回一个带有一些错误消息的json,但仍然返回一些东西)没有成功。

我做错了什么?

【问题讨论】:

    标签: ios8 nsurl nsurlrequest


    【解决方案1】:

    这就是我解决问题的方法。祝你好运!

    NSString *google = @"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=%f,%f&radius=500&types=%@&key=%@";  
    NSString *link = [NSString stringWithFormat:google, coordinate.latitude, coordinate.longitude, types, GOOGLE_KEY];
    NSURL *url = [NSURL URLWithString:[link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];  
    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];
    

    【讨论】:

    • 谢谢,百分号转义部分就够了!
    猜你喜欢
    • 2015-03-31
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    相关资源
    最近更新 更多