我到处寻找,似乎无法找到解决方案。我正在使用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是什么让有效的回复无法回来?
答案 0 :(得分:0)
这是因为您没有正确地传递V(date)
值,而请求您只是传递静态(旧),这可能在该日期之后无效。
您必须在URL字符串中传递更新(当前)V(date)
,V
应该是您正在向FourSquare发出请求的CurrentDate。
尝试使用以下代码并再次使用。
更新:一件事您应该传递一些东西进行搜索 因为在您的请求中您没有传递任何搜索查询
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] autorelease];
[dateFormatter setDateFormat:@"YYYYMMdd"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
//dateString it;s used for being UpTodate for API request
NSString* rest=@"street"
// To be Search
MakeRequest As Below和Code的其余部分将与您使用的相同。
NSString *foursquareURLString = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=%0.6f,%0.6f&client_id=%@&client_secret=%@&v=%@&query=%@",
latitude,
longitude,
FOURSQUARE_CLIENT_ID,
FOURSQUARE_CLIENT_SECRET,dateString,rest];
//Convert the Special characters into escapes.
foursquareURLString =[foursquareURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:foursquareURLString]];
//Rest will be same
For More Detail About the FourSquare API endpoints go Through This Link
我希望它能给你正确的结果而不是Null
!!!!!!!。