0

RestKit 0.20.1 を使用してサーバーからデータをプルする iOS アプリがあります。この時点で、サーバーは JSON 形式のデータを送信できますが、JSON 形式のデータを受信することはできません。

これが私の問題です。POST 要求には HTTP 本文が必要であり、サーバーは XML を受信するように設定されています。XML を受信できるように RKXMLReaderSerialization アドオンを実装しましたが、RestKit を使用して XML 形式の HTTP 本文を送信する現在の方法が見つかりません。

この質問 " 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];

postObject メソッドについて RestKit objectManager が言っていることは次のとおりです。

/**
 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.

JSONに設定した場合MIMEType、Trace は myrequest.bodyが so のように取り込まれていることを示しますrequest.body={"Search":"Trending"}

ただし、MIMETypeXML に設定すると、トレースは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 をすべて一緒に避けることができます。

4

1 に答える 1

1

このトピックに関する他の投稿で恐れられ、示唆されているように、XML の書き込みは RestKit 0.20.1 のバージョンではサポートされていません。これは、この件に関する Blake Watters との会話へのリンクです。

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

MIMEType が XML に設定されているときに request.body= (null) が発生する理由を明確にしませんでした。

サーバーの管理者は、JSON を受信するようにサーバーを設定することに同意しました。それが私がこれを回避する方法です。

于 2013-06-12T23:08:07.000 に答える