在我的应用中,我正在使用Google地图搜索用户旁边的地方。当我在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的断点指向它)。我的代码有什么问题或遗漏了什么?
答案 0 :(得分:0)
令人惊讶的是,在将网址更改为以下内容后,它正在运行:
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];
然而,我不明白为什么它依赖于此。任何人都可以向我解释一下吗?谢谢!并感谢所有在上述评论中做出贡献的人。