【发布时间】:2015-01-11 01:43:19
【问题描述】:
我正在尝试通过“http://www.raywenderlich.com/58682/introduction-restkit-tutorial”学习 RestKit,同时尝试了网站上提供的示例代码。好吧,我的主要问题是 RestKit 没有返回响应,即使我发送给 API 的请求是正确的,并且当我通过浏览器测试它时得到了响应。
这是来自网站的代码(唯一的变化是我发送了一些其他参数):
- (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 请求 URL:
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" = (
);
}>
而“操作”对象是:
<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>>
知道发生了什么吗?
附:要测试我提供的 URL,您需要将 kCLIENTID 和 kCLIENTSECRET 替换为您的客户端 ID 和客户端密码信息。
【问题讨论】:
-
你得到什么响应 JSON?打开跟踪日志,它显示什么?
标签: ios restkit foursquare