Google Places API未返回回复

cha*_*asi 5 ios google-places-api

我在线跟踪教程,我有两种方法可以向Google Places API提交查询.不幸的是,我试图回复,但它无法正常工作.我在代码中有一些调试号.但是,这是代码.

-(void) queryGooglePlaces{
    NSString *url = @"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=myKey";

    //Formulate the string as a URL object.
    NSURL *googleRequestURL=[NSURL URLWithString:url];
    NSLog(@"1.5");
    // Retrieve the results of the URL.
    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
    NSLog(@"2");
}

-(void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization 
                          JSONObjectWithData:responseData 

                          options:kNilOptions 
                          error:&error];

    //The results from Google will be an array obtained from the NSDictionary object with the key "results".
    NSArray* places = [json objectForKey:@"results"]; 

    //Write out the data to the console.
    NSLog(@"Google Data: %@", places);
    NSLog(@"3");
}
Run Code Online (Sandbox Code Playgroud)

在日志上输出如下:

2012-08-03 16:40:12.411 sCode[25090:1a303] 1.5
2012-08-03 16:40:12.411 sCode[25090:1a303] 2
2012-08-03 16:40:12.512 sCode[25090:1a303] 4
2012-08-03 16:40:12.751 sCode[25090:1a303] Google Data: (
)
2012-08-03 16:40:12.751 sCode[25090:1a303] 3
2012-08-03 16:40:13.628 sCode[25090:1a303] 1
2012-08-03 16:40:14.129 sCode[25090:1a303] 4
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我什么是错的,所以我没有得到回复.是的,我打电话给[self queryGooglePlaces];我的ViewDidLoad方法欣赏帮助家伙!对不起,如果我太详细..只是一个初学者试图学习!

Mar*_*ams 2

鉴于提供的代码,我最好的猜测是您需要通过添加百分比转义来对 URL 字符串进行编码。url尝试像这样创建...

NSString *url = @"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=myKey";
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Run Code Online (Sandbox Code Playgroud)

您通常会将其写成一行,但为了清楚起见,将其显示为两行。这会将这些=和转换&为正确转义的字符以提供有效的 URL。