1)我使用了Yelp API。網址爲特殊業務 - http://api.yelp.com/v2/business/ 全球搜索 - http://api.yelp.com/v2/search 搜索後,您必須正確地傳遞數據在API搜索網址。網址簽名通知NSStringWithFormat。並且不要忘記OAuth密鑰!我的要求:
-(void)searchBy:(NSString *)categoryFilter inLocationCity:(NSString *)aLocationCity {
NSString *urlString = [NSString stringWithFormat:@"%@?term=%@&location=%@",
YELP_SEARCH_URL,
categoryFilter,
aLocationCity];
NSURL *URL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:OAUTH_CONSUMER_KEY
secret:OAUTH_CONSUMER_SECRET];
OAToken *token = [[OAToken alloc] initWithKey:OAUTH_TOKEN
secret:OAUTH_TOKEN_SECRET];
id<OASignatureProviding, NSObject> provider = [[OAHMAC_SHA1SignatureProvider alloc] init];
NSString *realm = nil;
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:URL
consumer:consumer
token:token
realm:realm
signatureProvider:provider];
[request prepare];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
self.urlRespondData = [NSMutableData data];
}
}
還添加方法NSURLConnectionDelegate:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.urlRespondData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
[self.urlRespondData appendData:d];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *e = nil;
NSDictionary *resultResponseDict = [NSJSONSerialization JSONObjectWithData:self.urlRespondData
options:NSJSONReadingMutableContainers
error:&e];
if (self.resultArray && [self.resultArray count] > 0){
[self.resultArray removeAllObjects];
}
if (!self.resultArray) {
self.resultArray = [[NSMutableArray alloc] init];
}
DLog(@"YELP response %@", resultResponseDict);
if (resultResponseDict && [resultResponseDict count] > 0) {
if ([resultResponseDict objectForKey:@"businesses"] &&
[[resultResponseDict objectForKey:@"businesses"] count] > 0) {
for (NSDictionary *venueDict in [resultResponseDict objectForKey:@"businesses"]) {
Venue *venueObj = [[Venue alloc] initWithDict:venueDict];
[self.resultArray addObject:venueObj];
}
}
}
[self.delegate loadResultWithDataArray:self.resultArray];
}
有什麼不對的地方谷歌API?它迴應否? – 2014-10-28 06:25:40
是的,它給出了迴應,但給出了紐約市所有餐廳的名單。 我想在紐約搜索特定的餐廳「XYZ」。此外,我無法得到restuarant圖標。 – 2014-10-28 06:40:11