【发布时间】:2012-03-02 06:06:45
【问题描述】:
我到处寻找,似乎找不到解决方案。我正在使用 ASIHTTPRequest 在我的 iOS 应用程序中接入 FourSquare API。但是,当我尝试打印我希望返回给我的 JSON 字符串时,我得到“null”。如果我在浏览器中导航到相同的请求 URL,我会得到一大堆 JSON。这是我的代码...
- (void)fetchFoursquareLocationsUsingLocation:(CLLocation *)location {
// First, let's build our request
CLLocationDegrees latitude = location.coordinate.latitude;
CLLocationDegrees longitude = location.coordinate.longitude;
NSString *foursquareURLString = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=%0.6f,%0.6f&client_id=%@&client_secret=%@&v=20120103",
latitude,
longitude,
FOURSQUARE_CLIENT_ID,
FOURSQUARE_CLIENT_SECRET];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:foursquareURLString]];
self.foursquareData = [[NSMutableData alloc] init];
request.delegate = self;
[request startAsynchronous];
}
#pragma mark - ASIHTTPRequest Delegate
- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data {
[self.foursquareData appendData:data];
}
- (void)requestFinished:(ASIHTTPRequest *)request {
NSString *jsonCheck = [[NSString alloc] initWithData:self.foursquareData encoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonCheck);
}
更新:感谢@Kamarshad 的 SO 帖子 How to get Venues list Using FourSquare Api,我能够取回有效的 JSON。不同之处在于我异步发出请求,这样
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:foursquareURLString]];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//
if (error != nil) {
NSLog(@"Something went wrong...%@", [error localizedDescription]);
}
else {
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonString);
}
}];
ASIHTTPRequest 阻止有效响应返回的原因是什么??
【问题讨论】:
标签: iphone ios json asihttprequest foursquare