简而言之:我尝试使用
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
创造了,它是零!所以我假设RestKit只是放置了它自己默认的可接受的内容类型。。我只是不知道在哪里以及如何预防。有什么想法吗?
附笔
我无法控制服务器,所以我无法更改它的
内容类型
标头到
应用程序/JSON
更新:
事实证明
RKObjectRequestOperation.m
它得到了
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
内容类型?