0

在我的应用程序中,我正在使用谷歌地图搜索用户旁边的地方。当我在 URL 中仅包含一种类型的位置(例如食物)时,查询和解析 JSON 响应文件可以正常工作。/当我添加另一种类型(例如 food|bar)时,应用程序在 NSDictionary 行崩溃。这是代码:

NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&types=food|restaurant&sensor=true&key=%@", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude, [NSString stringWithFormat:@"%i", 1000], kGOOGLE_API_KEY];//|bar|cafe|casino|gym|amusement_park|night_club|park|restaurant|shopping_mall|stadium

    NSURL *requestURL = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:(requestURL)];

    //response
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSError *jsonParsingError = nil;
    NSDictionary *locationResults = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError]; //ERROR IS HERE

    NSLog(@"%@",locationResults);

我指出了错误发生的位置(Xcode 的断点指向它)。我的代码有什么问题或缺少什么吗?

4

1 回答 1

0

令人惊讶的是,在将 url 更改为以下内容后它正在工作:

    NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&types=food|hospital&sensor=true&key=%@", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude, [NSString stringWithFormat:@"%i", 1000], kGOOGLE_API_KEY];//|bar|cafe|casino|gym|amusement_park|night_club|park|restaurant|shopping_mall|stadium
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

但是,我不明白为什么它依赖于此。谁能给我解释一下?谢谢!并感谢所有在上述评论中做出贡献的人。

于 2013-07-23T16:21:08.410 回答