我有一个使用 RestKit 0.20.1 从服务器提取数据的 iOS 应用程序。此时的服务器可以发送 JSON 格式的数据,但无法接收 JSON 格式的数据。
这就是我的问题所在。POST 请求需要 HTTP 正文,并且服务器设置为接收 XML。我已经实现了 RKXMLReaderSerialization 插件,因此我可以接收 XML,但我找不到任何当前使用 RestKit 发送 XML 格式的 HTTP 正文的方法。
这个问题“ Send post request in XML format using RestKit ”是我一直在寻找的,但是由于 RestKit 的变化,Imran Raheem 的答案现在(据我所知)已经过时了。
我正在使用这种方法进行 POST
[[RKObjectManager sharedManager] postObject:nil path:@"/rest/search?ip=255.255.255.0" parameters:search success:nil failure:nil];
这是 RestKit objectManager 关于 postObject 方法的内容
/**
Creates an `RKObjectRequestOperation` with a `POST` request for the given object, and enqueues it to the manager's operation queue.
@param object The object with which to construct the object request operation. If `nil`, then the path must be provided.
@param path The path to be appended to the HTTP client's base URL and used as the request URL. If nil, the request URL will be obtained by consulting the router for a route registered for the given object's class and the `RKRequestMethodPOST` method.
@param parameters The parameters to be reverse merged with the parameterization of the given object and set as the request body.
@param success A block object to be executed when the object request operation finishes successfully. This block has no return value and takes two arguments: the created object request operation and the `RKMappingResult` object created by object mapping the response data of request.
@param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data. This block has no return value and takes two arguments:, the created request operation and the `NSError` object describing the network or parsing error that occurred.
如果我将MIMEType
设置设置为 JSON,则 Trace 会显示我request.body
正在像这样填充request.body={"Search":"Trending"}
。
但是,如果我将 设置MIMEType
为 XML,则 Trace 会显示request.body=(null)
这是我用来改变MIMEType
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
我对 iOS 和 Objective-C 很陌生,所以我可能设置了错误的 `postObject' 方法的参数中使用的 NSDictionary。这里以防万一......
NSArray *objects =[NSArray arrayWithObjects:@"trending", nil];
NSArray *keys =[NSArray arrayWithObjects: @"Search",nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
self.search=params;
任何帮助将不胜感激!鉴于我是新的 Snippets 特别有帮助!
哦,顺便说一句,如果有人能指出我接受 JSON 输入的 REST 方法,我很乐意将它传递给我的服务器人员,这样我就可以一起避免使用 XML。