我有一个项目,当我建立我的RestEngine类时,我正在设置所有的responseDescriptors(其中8个)和映射(其中8个)...我提前设置它们而不是在每个调用期间。 我不确定这种做法是否合适。
问题是我的一个描述符似乎在不应该映射所有结果。
我认为问题在于我的thingDescriptor,或者它可能是以下路由(v1 / things /:thingID \ .json),但这条路线是GET所以我不认为我的POST请求会去通过这条路线?
这是我正在进行的POST请求,它将结果映射到特定的CLASS,它不应该是,或者我不希望它被映射到特定的类。
[[RKObjectManager sharedManager] postObject:nil
path:@"v1/things/request.json"
parameters:@{
@"auth_token" : self.accessToken,
@"api_key" : self.api_key,
@"lat" : lat,
@"lng" : lng
}
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Post request of an Thing Request start tracking request update...");
<-- the mappingResult shows its mapped to a Thing class but I want server response to mapped to an NSDictionary instead?
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Post request of an Thing Request failed with error: %@", error);
}];
以下是我设置的映射:
// Routes for Things
[objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[Thing class]
pathPattern:@"v1/things/:thingID\\.json"
method:RKRequestMethodGET]];
[objectManager.router.routeSet addRoute:[RKRoute routeWithName:kRouteUpdateThingLoc
pathPattern:@"v1/things/update_location.json"
method:RKRequestMethodPOST]];
[objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[Thing class]
pathPattern:@"v1/things.json"
method:RKRequestMethodPOST]];
// Register our mappings with the provider
RKResponseDescriptor *thingResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:thingMapping
pathPattern:@"v1/things.json"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKResponseDescriptor *thingDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:thingMapping
pathPattern:@"v1/things/:thingID.json"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
答案 0 :(得分:1)
像这样使用AFNetworking:
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:@"v1/"];
NSDictionary *parameters = @{
@"auth_token" : self.accessToken,
@"api_key" : self.api_key,
@"lat" : lat,
@"lng" : lng
};
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"things/request.json" parameters:parameters];
AFJSONRequestOperation *opertaion = [AFJSONRequestOperation JSONRequestOperationWithRequest:responseObject success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"THIS IS A DICTIONARY (or array): %@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"if requestFailed");
}];
答案 1 :(得分:0)
上面有一些错别字:
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[[NSURL alloc] initWithString:@"http://my.herokuapp.com/"]];
NSDictionary *parameters = @{
@"api_key" : self.api_key,
@"acode" : code
};
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"v1/users/approve_by_code.json" parameters:parameters];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"THIS IS A DICTIONARY (or array): %@", JSON);
object:nil];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"if requestFailed");
}];
[operation start];