8

要するにcontent-type、httpリクエストヘッダーを@"text/html..に設定してサーバーからデータをフェッチしようとしましたが、何らかの理由でRestKitがそれをapplication/JSON

説明: AFNetworking だけを使用してこの要求を行う場合.. 物事は魅力的に機能します.. これは私の AFNetworking コードがどのように見えるかです:

AFHTTPClient *client = [AFHTTPClient alloc] initWithBaseURL:
                                               [NSURL URLWithString:kApiBaseUrl]];
singleton.parameterEncoding = AFJSONParameterEncoding;
[singleton setDefaultHeader:@"Accept" value:@"text/html"];
[client getPath:getPath parameters:nil success:successCallback failure:failureCallback];

まったく同じクライアントを使用して接続すると、

MyClient *client = [MyClient getSingleton];  //MyClient is instantiated as above        
self.objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
self.objectManager.managedObjectStore = self.managedObjectStore;
// this should have already been done by my client, but putting
// it here just to be sure
[self.objectManager setAcceptHeaderWithMIMEType:@"text/html"];

[[RKObjectManager sharedManager] getObjectsAtPath:kGradesPath 
                                       parameters:nil 
                                          success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
 // handle success
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
 // handle failure
}];

私が得るエラーは次のとおりです。

 restkit.network:RKObjectRequestOperation.m:576 Object request failed: 
 Underlying HTTP request operation failed with error: Error
 Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type {(
    "application/x-www-form-urlencoded",
    "application/json"
)}, got text/html" UserInfo=0x8f7acd0

件名を掘り下げます.. managedObjectRequestOperationWithRequestにブレークポイントを置き、作成されたのacceptableContentTypesをチェックしましたHTTPRequestOperationが、nilです! したがって、RestKit は、独自のデフォルトの許容可能なコンテンツ タイプを設定しているだけだと想定しています。アイデア?

psサーバーを制御できないため、content-typeヘッダーを変更できませんapplication/JSON


アップデート:

RKObjectRequestOperation.mmime-typefrom [RKMIMETypeSerialization registeredMIMETypes];(354 行目) を取得することがわかります。したがって、RKMIMETypeSerialization.hは次のメソッドがあります。

/**
 Registers the given serialization class to handle content for the given MIME Type identifier.

 MIME Types may be given as either a string or as a regular expression that matches the MIME Types for which the given serialization should handle. Serializations are searched in the reverse order of their registration. If a registration is made for an already registered MIME Type, the new registration will take precedence.

 @param serializationClass The class conforming to the RKSerialization protocol to be registered as handling the given MIME Type.
 @param MIMETypeStringOrRegularExpression A string or regular expression specifying the MIME Type(s) that given serialization implementation is to be registered as handling.
 */
+ (void)registerClass:(Class<RKSerialization>)serializationClass forMIMEType:(id)MIMETypeStringOrRegularExpression;

text/htmlこれを使用してコンテンツ タイプを登録するにはどうすればよいですか?

4

2 に答える 2

30

通常、RestKit は、応答データに対して単一の MIMEType (JSON) を想定しています。ただし、見つけたメソッドを使用して、他のタイプを処理するように指示できます。私の経験では、非常に便利ですtext/plaintext/htmlこれを RestKit 構成 (アプリ デリゲートで行う) に追加すると、 と の両方を応答データのコンテンツ タイプとして受け入れることができapplication/jsonますtext/html

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"];

私の場合、Jersey (私の会社のバックエンド チームが使用する Web サービス フレームワーク) は空のペイロードの content-type をデフォルトで に設定しているため、これも役立ちますtext/plain

お役に立てれば。

于 2013-10-26T07:40:14.367 に答える
-1

このようにサーバーAPIから受け取るタイプに変更const値を使用します

NSString * const RKMIMETypeJSON = @"text/html";

JSONと同じテキスト構造を受け取った場合、このアプローチは完璧に機能します

于 2015-07-14T23:13:48.983 に答える