Restkit Get with Params & Block with Shared Client

﹥>﹥吖頭↗ 提交于 2019-12-12 17:08:32

问题


It won't let me attached the params to the request, what am I doing wrong? Params is a Dictionary and endString adds to the 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);
    };
}];

I get this error:RestKit was asked to retransmit a new body stream for a request. Possible connection error or authentication challenge?


回答1:


I think you are on the right track. Below is from a working example I found here, about 2/3 the way down the page. Another option for you may be to append the params directly to the URL. I'm not sure if that's feasible for you, but if your parameters are simple then it may be.

- (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;
    }];
}

You should also take a look at this SO question: RestKit GET query parameters.



来源:https://stackoverflow.com/questions/12185251/restkit-get-with-params-block-with-shared-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!