[英]FourSquare request returns null string in iOS
我到处都是,似乎找不到解决方法。 我正在使用ASIHTTPRequest来进入我的iOS应用程序中的FourSquare API。 但是,当我尝试打印希望返回给我的JSON字符串时,我得到的是“空”。 如果在浏览器中导航到相同的请求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 post 如何获取场地列表使用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有什么作用,可防止返回有效的响应?
这是因为您在请求时未正确传递V(date)
值,而您只是传递了在该日期之后可能无法使用的static(old)。 您将必须在URL字符串中传递Updated(current) 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和本代码的其余部分将与您使用的相同。
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
有关FourSquare API端点的更多详细信息,请通过此链接
我希望它将为您提供正确的结果,而不是Null
!!!!!!!。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.