制作后与RKParams API与制图RKObjectMapping使用RestKit响应 [英] Making a post to API with RKParams and mapping the response with RKObjectMapping using RestKit

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

问题描述

现在我在做一个巨大的难治到我的code,我想结合我使用Restkit的方式。我已分居使RPC调用到我的API服务器的方式和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.

REST调用,其中使用objectMapping和RPC调用凡与RKClient制成的。除此之外,我使用的是块而不是代表,这是伟大的,但我有关于它的工作原理有些疑惑。

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.

下面是code我收到了,它的工作很好地手动发布对象,并进行映射使用代理,之后是利用块不发送PARAMS新的code

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

    }];

}

我要送请求有它的身体空的,这意味着,PARAMS是不是sendend或适当设置好的。关于如何得到这个任何想法来工作?

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;

在code是如下:

the code is as follows:

- (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与制图RKObjectMapping使用RestKit响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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