为什么RestKit改变我的响应内容types?

简而言之:我尝试从HTTP请求头的content-type设置为@"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创build的acceptableContentTypes ,它是零! 所以我假设RestKit只是把自己的默认可接受的内容types..我只是不知道在哪里,以及如何防止它。 想法?

PS我没有控制服务器,所以我不能改变它的application/JSONcontent-type


更新:

事实certificate,在RKObjectRequestOperation.m中,它从[RKMIMETypeSerialization registeredMIMETypes];获取了mime-type [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内容types?

RestKit通常需要一个MIMEType(JSON)作为响应数据。 但是,您可以使用您find的方法来告诉它处理其他types的text/plain text/htmltext/html ,根据我的经验,它非常方便。 添加到我的RestKitconfiguration(我在我的应用程序委托中)允许我接受application/jsontext/html作为响应数据内容types。

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

就我而言,这也是有帮助的,因为泽西 – 我公司的后端团队使用的Web服务框架 – 将空有效内容的内容types默认为text/plain ,这会触发失败块,除非我已经专门注册了MIMEType。

希望这可以帮助。

我使用更改常量值来键入从服务器API这样recive

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

如果recurrent的文本结构与JSON相同,这个方法就完美了