使用 RKParams 发布到 API 并使用 RestKit 使用 RKObjectMapping 映射响应 [英] Making a post to API with RKParams and mapping the response with RKObjectMapping using RestKit

查看:25
本文介绍了使用 RKParams 发布到 API 并使用 RestKit 使用 RKObjectMapping 映射响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在对我的代码进行大量重构,我想统一我使用 Restkit 的方式.我对 API 服务器的 RPC 调用和 REST 调用进行了分离.

Right now I'm doing a huge refactory to my code and I want to unify the way I'm using Restkit. I had separated ways of making RPC calls to my API server and REST calls.

使用 objectMapping 进行的 REST 调用和使用 RKClient 进行的 RPC 调用.除此之外,我使用的是块而不是委托,这很好,但我对它的工作原理有些怀疑.

REST calls where made using objectMapping and RPC calls where made with the RKClient. Besides that, I'm using blocks instead of delegates, which is great but I have some doubts about how it works.

这是我之前的代码,它可以很好地发布对象并使用委托手动进行映射,然后是使用不发送参数的块的新代码.

Here is the code I had before, which worked nicely to post the object and do the mapping manually using delegates and after that is the new code using blocks that does not send the params.

//This was the old way...
- (void) upload: (KFMedia *) pic {

    RKParams* imageParams = [RKParams params];
    NSData* imageData = UIImageJPEGRepresentation(pic.image, 0.7f);
    [imageParams setData:imageData MIMEType:@"image/jpg" forParam:@"FileUpload"];

    [[RKClient sharedClient] post:@"/api/upload/" params:imageParams delegate:self]; 

}

//This is the new way I'm trying...
- (void) upload: (KFMedia *) pic onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{

    RKParams* imageParams = [RKParams params];
    NSData* imageData = UIImageJPEGRepresentation(pic.image, 0.7f);
    [imageParams setData:imageData MIMEType:@"image/jpg" forParam:@"FileUpload"];

    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/api/upload/" usingBlock:^(RKObjectLoader *loader) {

        //Trying to set params here, but it seems that I'm not sending anything :(
        loader.params = imageParams;
        loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[KFMedia class]];
        loader.delegate = self;
        loader.onDidLoadObject = loadBlock;
        loader.onDidFailWithError = failBlock;
        loader.onDidFailLoadWithError = failBlock;
        loader.onDidLoadResponse = ^(RKResponse *response) {
            [self fireErrorBlock:failBlock onErrorInResponse:response];
        };

    }];

}

我发送的请求的正文为空,这意味着未正确发送或设置参数.关于如何让它发挥作用的任何想法?

The request I'm sending has its body empty, that means that the params are not being sendend or setted properly. Any ideas on how to get this to work?

推荐答案

我刚刚解决了这个问题.要使用 loadObjectsAtResourcePath 发送额外的参数,您必须使用

I just solved the problem. To send the extra params with loadObjectsAtResourcePath you must force a post using

loader.method = RKRequestMethodPOST;

代码如下:

- (void) upload: (KFMedia *) pic onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{

    RKParams* imageParams = [RKParams params];
    NSData* imageData = UIImageJPEGRepresentation(pic.image, 0.7f);
    [imageParams setData:imageData MIMEType:@"image/jpg" forParam:@"FileUpload"];

    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/api/upload/" usingBlock:^(RKObjectLoader *loader) {

        loader.method = RKRequestMethodPOST; //This line solved the problem
        loader.params = imageParams;
        loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[KFMedia class]];
        loader.delegate = self;
        loader.onDidLoadObject = loadBlock;
        loader.onDidFailWithError = failBlock;
        loader.onDidFailLoadWithError = failBlock;
        loader.onDidLoadResponse = ^(RKResponse *response) {
            [self fireErrorBlock:failBlock onErrorInResponse:response];
        };

    }];

}

这篇关于使用 RKParams 发布到 API 并使用 RestKit 使用 RKObjectMapping 映射响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆