2013-06-01 61 views
0

我有一個iOS應用程序使用RestKit 0.20.1從服務器提取數據。服務器此時可以發送JSON格式的數據,但無法接收JSON格式的數據。如何使用RestKit發送XML格式的POST請求0.20.1

這是我的問題所在。 POST請求需要HTTP Body,並且服務器設置爲接收XML。我已經實現了RKXMLReaderSerialization add,因此我可以接收XML,但是我找不到任何使用RestKit發送XML格式的HTTP Body的當前方式。

這個問題「Send post request in XML format using RestKit」是我正在尋找的,但現在Imran Raheem的答案(據我所知)由於RestKit的變化而過時。

我使用這種方法爲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跟蹤顯示我request.body正在像request.body={"Search":"Trending"}那樣填充。

但是如果我設置MIMEType到XML中的跟蹤顯示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; 

任何幫助將不勝感激!鑑於我是新的片段特別有用!

哦,順便說一句,如果任何人都可以指向我接受JSON輸入的REST方法,我會很樂意將它傳遞給我的服務器人,這樣我就可以避免所有的XML。

回答

1

由於擔心和建議在這個主題的其他帖子寫的XML不支持版本的RestKit 0.20.1。這裏是我與Blake Watters就這個問題進行對話的鏈接。

https://github.com/RestKit/RestKit/issues/1430#issuecomment-19150316

爲什麼request.body =(空)當MIME類型設置爲XML我從來沒有清晰起來。

服務器的管理員已同意將其設置爲接收JSON。這就是我計劃解決這個問題的方法。