我正在尝试使用 restKit 发出 POST 请求,但我不能。
我的代码是这样的:
NSString *urlAddTask;
urlAddTask = [NSString stringWithFormat:@"some URL"];
RKObjectManager* mgr = [RKObjectManager sharedManager];
mgr.serializationMIMEType = RKMIMETypeJSON;
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping mapKeyPath: @"name" toAttribute:@"name" ];
RKObjectMapping* mappingForSerialization = [mapping inverseMapping];
[mgr.mappingProvider setSerializationMapping:mappingForSerialization forClass:[NSManagedObject class]];
[mgr.router routeClass:[NSManagedObject class] toResourcePath:urlAddTask forMethod:RKRequestMethodPOST];
Task *t=[[Task alloc]init];
t.name=@"One name";
[[RKObjectManager sharedManager].router routeClass:[Task class] toResourcePath:@"/tasks" forMethod:RKRequestMethodPOST];
[mgr postObject:t delegate:nil/*self*/];
当我执行此代码时,我收到此错误:
*** Assertion failure in -[RKObjectLoader prepareURLRequest], /myAppRoute/Libraries/RestKit/Code/ObjectMapping/RKObjectLoader.m:304
2013-02-14 17:12:25.202 Aplication[1929:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'You must provide a serialization mapping for objects of type 'Task''
我觉得我做了很多坏事。我正在寻找一个关于如何发布帖子的好例子,但我没有找到任何可以帮助我完成请求的帖子。
你能帮我写代码吗,或者给我一个很好的例子来说明如何做到这一点?
谢谢!
更新
我像你说的那样更新了序列化行。然后我有这个错误:
2013-02-15 10:16:45.009 myApp[829:c07] W restkit.network:RKObjectLoader.m:241 Unable to find parser for MIME Type 'text/html'
2013-02-15 10:16:45.009 myApp[829:c07] W restkit.network:RKObjectLoader.m:262 Encountered unexpected response with status code: 200 (MIME Type: text/html)
然后我尝试使用以下行将我的 serializationMIMEType 从 RKMIMETypeJSON 更改为 RKMIMETypeXML:
mgr.serializationMIMEType = RKMIMETypeXML;
现在错误是这样的:
2013-02-15 10:20:01.315 myApp[873:c07] -[RKXMLParserLibXML stringFromObject:error:]: unrecognized selector sent to instance 0x86ae910
2013-02-15 10:20:01.316 myApp[873:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RKXMLParserLibXML stringFromObject:error:]: unrecognized selector sent to instance 0x86ae910'
我怎么说我确定我做了很多坏事。我很迷茫。
谢谢!!