简而言之:我尝试使用以下命令从服务器获取数据content-type
http请求头设置为@"text/html
..但由于某种原因 RestKit 将其更改为application/JSON
解释:如果我只使用AF网络 https://github.com/AFNetworking/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
深入研究主题..我在其中设置了一个断点托管对象请求操作与请求 https://github.com/RestKit/RestKit/blob/development/Code/Network/RKObjectManager.m?source=c,然后我检查了可接受的内容类型 https://github.com/RestKit/RestKit/blob/development/Code/Network/RKHTTPRequestOperation.h?source=c of HTTPRequestOperation
创建了,它是零!所以我假设 RestKit 只是放置它自己的默认可接受的内容类型..我只是不知道在哪里以及如何阻止它。想法?
p.s.我无法控制服务器,所以我无法更改它content-type
标头至application/JSON
Update:
事实证明,在RKObjectRequestOperation.m https://github.com/RestKit/RestKit/blob/development/Code/Network/RKObjectRequestOperation.m?source=c它得到mime-type
from [RKMIMETypeSerialization registeredMIMETypes];
(第354行)..等等RKMIMETypeSerialization.h https://github.com/RestKit/RestKit/blob/development/Code/Support/RKMIMETypeSerialization.h?source=cc有一个方法:
/**
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
内容类型?