Google Places API未返回回复

时间:2012-08-06 17:01:15

标签: ios google-places-api

我正在关注Tutorial在线,我有两种方法可以向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");
}

在日志中输出如下:

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

有人能告诉我哪里出错了所以我没有得到回复。 是的,我在[self queryGooglePlaces];方法中拨打了ViewDidLoad 感谢帮助人!对不起,如果我太详细了......只是一个尝试学习的初学者!

1 个答案:

答案 0 :(得分: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];

您通常会将其划分为一行,但为了清晰起见,将其显示为两行。这会将=&转换为正确转义的字符,以提供有效的网址。