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中设置了一个断点,然后我检查了创建的可接受HTTPRequestOperation内容类型,结果为零!所以我假设 RestKit 只是把它自己的默认可接受的内容类型.. 我只是不知道在哪里以及如何防止它。想法?

ps我无法控制服务器,所以我无法将其content-type标题更改为application/JSON


更新:

事实证明,在RKObjectRequestOperation.m中它得到了mime-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 generally expects a single MIMEType (JSON) for its response data. However, you can tell it to handle other types like text/plain and text/html using the method you found and in my experience it's been pretty handy. Adding this to my RestKit config (which I do in my app delegate) allows me to accept both application/json and text/html as response data content-types.

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

In my case, this is also helpful because Jersey - the web services framework the backend team at my company uses - defaults the content-type of empty payloads to text/plain, which triggers failure blocks unless I've specifically registered for that MIMEType.

Hope this helps.

于 2013-10-26T07:40:14.367 回答
-1

我使用 change const value 来键入从服务器 api 接收的内容,如下所示

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

如果接收的文本结构与 JSON 相同,则此方法非常有效

于 2015-07-14T23:13:48.983 回答