RKObjectLoader用数组参数截断我的路径

时间:2012-05-11 12:38:01

标签: objective-c ios rest restkit

我的RestKit有问题

我尝试发送参数字符串数组。这样做。

RKObjectMapping* tagMapping = [[RKObjectManager sharedManager].mappingProvider
    objectMappingForClass:[RKTag class]];
NSArray *tags = [NSArray arrayWithObjects:@"Home", @"Relation", nil];
NSDictionary *dictParams =
    [NSDictionary dictionaryWithObject:tags forKey:@"type"];

NSString *resourcePath = [[NSString stringWithString:@"/tags"]
    stringByAppendingQueryParameters:dictParams];
[_m loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader) {
    [loader setObjectMapping:tagMapping];
    [loader setMethod:RKRequestMethodGET];
    [loader setDelegate:delegate];
}];

但是,当我在服务器上看到控制台时,我看到了

  

开始GET“/标签?类型%5B%5D =%D0%A0%D0%BE%D0%B4%D1%81%D1%82%D0%B2%D0%B5%D0%BD%D0% BD%D1%8B%D0%B5%20%D0%BE%D1%82%D0%BD%D0%BE%D1%88%D0%B5%D0%BD%D0%B8%D1%8F“for 127.0 .0.1于2012-05-11 16:30:54 +0400   由TagsController处理#index为JSON     参数:{“type”=> [“Home”]}

当RKObjectLoader初始化时,调用方法loaderWithResourcePath,截断我的数组

如何解决?

1 个答案:

答案 0 :(得分:0)

我通过更改RKUrlClass

解决了这个问题
- (id)initWithBaseURL:(NSURL *)theBaseURL resourcePath:(NSString *)theResourcePath queryParameters:(NSDictionary *)theQueryParameters {
    NSDictionary *resourcePathQueryParameters = [theResourcePath queryParameters];
    NSMutableDictionary *mergedQueryParameters = [NSMutableDictionary dictionaryWithDictionary:[theBaseURL queryParameters]];
    [mergedQueryParameters addEntriesFromDictionary:resourcePathQueryParameters];
    [mergedQueryParameters addEntriesFromDictionary:theQueryParameters];

    // Build the new URL path
    NSRange queryCharacterRange = [theResourcePath rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"?"]];
    NSString *resourcePathWithoutQueryString = (queryCharacterRange.location == NSNotFound) ? theResourcePath : [theResourcePath substringToIndex:queryCharacterRange.location];
    NSString *baseURLPath = [[theBaseURL path] isEqualToString:@"/"] ? @"" : [[theBaseURL path] stringByStandardizingPath];
    NSString *completePath = resourcePathWithoutQueryString ? [baseURLPath stringByAppendingString:resourcePathWithoutQueryString] : baseURLPath;
    NSString* completePathWithQuery = [completePath stringByAppendingQueryParameters:mergedQueryParameters];

    // before
    //    NSURL* completeURL = [NSURL URLWithString:completePathWithQuery relativeToURL:theBaseURL];
    // after my edit
    NSString *cPath = theResourcePath ? theResourcePath : baseURLPath;
    NSURL* completeURL = [NSURL URLWithString:cPath relativeToURL:theBaseURL];

    if (!completeURL) {
        RKLogError(@"Failed to build RKURL by appending resourcePath and query parameters '%@' to baseURL '%@'", theResourcePath, theBaseURL);
        [self release];
        return nil;
    }

    self = [self initWithString:[completeURL absoluteString]];
    if (self) {
        self.baseURL = theBaseURL;
        self.resourcePath = theResourcePath;
    }

    return self;
}