Я искал везде и, кажется, не могу найти решения. Я использую ASIHTTPRequest, чтобы подключиться к FourSquare API в моем приложении для iOS. Однако, когда я пытаюсь распечатать строку 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);
}
ОБНОВЛЕНИЕ: благодаря сообщению SO @ Kamarshad Как получить места list Используя 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, который не дает правильному ответу вернуться?