我在 iOS 平台的 0.20.x 版本中看到了 RestKit 框架的许多重大变化。 到目前为止我还没有在网上找到的一件事是如何使用新版本的 RestKit 下载二进制文件的示例。
我需要将一个 JSON 对象发送到 REST 服务,并期望返回一个二进制文件。看起来很简单,不是吗,但出于某种原因,RestKit 只希望返回 JSON(以及常见的 Internet 内容类型,如 XML)。 JSON 对象本质上是一个请求对象,它告诉服务它应该去为我获取哪个图像。
最佳答案
幸运的是,我已经设法使用底层的 AFNNetworking 框架来帮助我解决这个问题,并利用 RestKit 序列化程序来生成我需要的请求对象。
MyRequestClass *request = // ... get my request class instance
RKObjectManager *manager = [RKObjectManager sharedManager];
NSMutableURLRequest *downloadRequest = [manager requestWithObject:request method:RKRequestMethodPOST path:ROUTE_URL_MY_SERVICE parameters:nil];
AFHTTPRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:downloadRequest];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Use my success callback with the binary data and MIME type string
callback(operation.responseData, operation.response.MIMEType, nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Error callback
callback(nil, nil, error);
}];
[manager.HTTPClient enqueueHTTPRequestOperation:requestOperation];
关于ios - 使用 RestKit 0.20.x 下载二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18429397/