2

它不会让我将参数附加到请求中,我做错了什么?Params 是一个 Dictionary 并且 endString 添加到 sharedClient baseURL。

[[RKClient sharedClient] get:endString usingBlock:^(RKRequest *loader){
    loader.params = [RKParams paramsWithDictionary:params];
    loader.onDidLoadResponse = ^(RKResponse *response) {

        [self parseJSONDictFromResponse:response];
    };


    loader.onDidFailLoadWithError = ^(NSError *error) {
        NSLog(@"error2:%@",error);
    };
}];

我收到此错误:RestKit was asked to retransmit a new body stream for a request. Possible connection error or authentication challenge?

4

1 回答 1

0

我认为你在正确的轨道上。下面是我在这里找到的一个工作示例,大约在页面下方的 2/3 处。您的另一个选择可能是将参数直接附加到 URL。我不确定这对你是否可行,但如果你的参数很简单,那么它可能是。

- (void)authenticateWithLogin:(NSString *)login password:(NSString *)password onLoad:(RKRequestDidLoadResponseBlock)loadBlock onFail:(RKRequestDidFailLoadWithErrorBlock)failBlock
{
    [[RKClient sharedClient] post:@"/login" usingBlock:^(RKRequest *request) {
        request.params = [NSDictionary dictionaryWithKeysAndObjects:
                          @"employee[email]", login,
                          @"employee[password]", password,
                          nil];
        request.onDidLoadResponse = ^(RKResponse *response) {

            id parsedResponse = [response parsedBody:NULL];
            NSString *token = [parsedResponse valueForKey:@"authentication_token"];
            //NSLog(@"response: [%@] %@", [parsedResponse class], parsedResponse);

            if (token.length > 0) {
                NSLog(@"response status: %d, token: %@", response.statusCode, token);
                [[RKClient sharedClient] setValue:token forHTTPHeaderField:@"X-Rabatme-Auth-Token"];

                if (loadBlock) loadBlock(response);
            }

            [self fireErrorBlock:failBlock onErrorInResponse:response];
        };
        request.onDidFailLoadWithError = failBlock;
    }];
}

您还应该看看这个 SO 问题:RestKit GET query parameters

于 2012-08-29T20:54:18.813 回答