1

我有一个项目,当我的 RestEngine 类建立时,我正在设置所有 responseDescriptors(其中 8 个)和映射(其中 8 个)......我提前设置它们,而不是在每次调用期间。我不确定这是否是好的做法。

问题是我的一个描述符似乎在不应该映射所有结果。

我认为问题在于我的 thingDescriptor 或者它可能是以下路线(v1/things/:thingID\.json),但这条路线是 GET,所以我认为我的 POST 请求不会通过这条路线?

这是我正在发出的 POST 请求,该请求将结果映射到不应该的特定类,或者我不希望它映射到特定类。

[[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)];
4

2 回答 2

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");
    }];
于 2013-05-01T02:20:27.320 回答
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];
于 2013-06-10T19:42:14.727 回答