我正在尝试通过“http://www.raywenderlich.com/58682/introduction-restkit-tutorial”学习RestKit,并同时尝试在网站上提供的示例代码。我的主要问题是,即使我发送给API的请求是正确的,RestKit也没有返回响应,当我通过浏览器测试时,我得到了响应。
以下是该网站的代码(只有更改是我发送的其他参数):
- (void)configureRestKit
{
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:@"https://api.foursquare.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
// setup object mappings
RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Venue class]];
[venueMapping addAttributeMappingsFromArray:@[@"name"]];
// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:venueMapping
method:RKRequestMethodGET
pathPattern:@"/v2/venues/search"
keyPath:@"response.venues"
statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
}
- (void)loadVenues
{
NSString *near = @"Chicago, IL";
NSString *clientID = kCLIENTID;
NSString *clientSecret = kCLIENTSECRET;
NSDictionary *queryParams = @{@"near" : near,
@"client_id" : clientID,
@"client_secret" : clientSecret,
@"categoryId" : @"4bf58dd8d48988d1e0931735",
@"v" : @"20140118"};
[[RKObjectManager sharedManager] getObjectsAtPath:@"/v2/venues/search"
parameters:queryParams
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
_venues = mappingResult.array;
[self.tableView reloadData];
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"What do you mean by 'there is no coffee?': %@", error);
}];
}
以下是API请求网址:
https://api.foursquare.com/v2/venues/search?categoryId=4bf58dd8d48988d1e0931735&client_id=kCLIENTID&client_secret=kCLIENTSECRET&near=Chicago%2C%20IL&v=20150110
成功块中我的'mappingResult'对象将出现:
<RKMappingResult: 0x7cb4f6d0, results={
"response.venues" = (
);
}>
和'operation'对象是:
<RKObjectRequestOperation: 0x78e697c0, state: Successful, isCancelled=NO, request: <NSMutableURLRequest: 0x78e4f2f0> { URL: https://api.foursquare.com/v2/venues/search?categoryId=4bf58dd8d48988d1e0931735&client_id=kCLIENTID&client_secret=kCLIENTSECRET&near=Chicago%2C%20IL&v=20150110 }, response: <NSHTTPURLResponse: 0x79a46e40 statusCode=200 MIMEType=application/json length=35120>>
知道发生了什么事吗?
P.S。要测试我提供的URL,您需要将kCLIENTID和kCLIENTSECRET替换为您的客户端ID和客户端密钥信息。
答案 0 :(得分:0)
将queryParams更改为
NSString *ll = @"40.7,-74.20";
NSDictionary *queryParams = @{@"ll": ll,
@"client_id" : clientID,
@"client_secret" : clientSecret,
@"categoryId" : @"4bf58dd8d48988d1e0931735",
@"v": @"20140118"
};