English 中文(简体)
用 RKParams 向 API 发送邮件, 用 RestKit 绘制 RKObject Mapping 响应图
原标题:Making a post to API with RKParams and mapping the response with RKObjectMapping using RestKit

现在,我正在对我的代码做一个巨大的重新构思,我想统一我使用雷斯基特的方式。我已经将RPC的通话方式与我的API服务器和REST的通话方式区分开来。

使用物体绘图和 RPC 调用RKClient 调用RCC。 此外,我使用街区而不是代表,这是件好事,但我对它如何运作有些怀疑。

这是我之前的代码, 它很好地张贴了对象, 并用代表手动绘制地图, 之后是使用不发送参数的区块的新代码。

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

    }];

}

我发送的请求是空的,这意味着参数没有被正确发送或设置。 对于如何使参数发挥作用有什么想法?

最佳回答

我刚刚解决了问题。 要发送加载的附加参数ObjectsAtResourtPath 您必须强制使用

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

    }];

}
问题回答

暂无回答




相关问题
C# Networking API s [closed]

Lately I ve been looking for a good networking API i could possibly use and/or reference some of the code within, but i have mere luck searching for some on Google/Bing. Hopefully somebody here has ...

getting XML from other domain using ASP.NET

I m fairly new to ASP.NET. And I was wondering how I could go about getting xml from a site (Kuler s API in this case), and then post the result using AJAX? So what I want here, is to be able to do a ...

Most appropriate API for URL shortening service

I ve just finished an online service for shortening URLs (in php5 with Zend Framework); you can enter an URL and you get an short URL (like tinyurl and such sites). I m thinking about the API for ...

UML Diagram to Model API

I need to create a diagram to document a RESTFul API that build, which UML diagram should I use? Thanks in advance,

How best to expose Rails methods via an API?

Let s say I have a model foo, and my model has a publish! method that changes a few properties on that model and potentially a few others too. Now, the Rails way suggests that I expose my model over ...

简讯

我是否可以使用某些软件来建立简便的航天国家服务器,最好是在 Java? 所有我都希望我的航天国家服务机在任何要求中都用同样的IP地址来回答。

About paypal express checkout api

In this picture,there are 3 main steps:SetExpressCheckout,GetExpressCheckoutDetails and DoExpressCheckoutDetails,I m now sure SetExpressCheckout is to be called by myself,what about ...

热门标签