首先,我将请求对象映射添加到管理器:
RKObjectMapPing *requestMapPing = [RKObjectMapPing requestMapPing]; [requestMapPing addAttributeMapPingsFromDictionary:@{ @"ID" : @"ID",@"name" : @"name",@"latitude" : @"latitude",@"longitude" : @"longitude" }]; RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapPing:requestMapPing objectClass:[Location class] rootKeyPath:nil]; [manager addRequestDescriptor:requestDescriptor];
然后我发出请求:
RKManagedobjectRequestoperation *operation = [RKObjectManager.sharedManager appropriateObjectRequestoperationWithObject:self method:RKRequestMethodPOST path:@"/API/v1/users/3/locations" parameters:@{@"token" : token}];[operation setCompletionBlockWithSuccess:^(RKObjectRequestoperation *operation,RKMapPingResult *mapPingResult) { Location * location = (Location*)mapPingResult; self.ID = Location.ID;} failure:^(RKObjectRequestoperation *operation,NSError *error) { ALog(@"fail!"); }];[RKObjectManager.sharedManager enqueueObjectRequestoperation:operation];
发出请求时,Location对象被序列化为JsON并放入请求体中.但是,不是将标记添加到查询字符串中,而是将其作为JsON添加到请求正文中.
例:
request.body={"ID":0,name="test","longitude":-0.1337,"latitude":51.50998,"token":"Z3JlZ2c6MTM2MDU2OTk2MDY2OTpMajkxd01acWxjcGg1dEpFVy9IaEcwNTcyMWJkSEpnTFRTQTI2eXNlN29VOVRTc1UwV1lEU0E9PQ=="}
任何帮助是极大的赞赏!
解决方法 在 https://gist.github.com/onelittlefish/5970616有一个Gist,为RKObjectManager提供了一个很好的扩展,允许你向PUT或POST请求添加查询参数.只需将这些文件放入项目中,导入标题,然后就可以使用类似@giuseppe的答案(将params添加到正文,而不是路径).唯一的区别是将参数更改为queryParameters – 您的调用可能如下所示:
[objectManager postObject:self path:@"/API/v1/users/3/locations" queryParameters:queryParams success:^(RKObjectRequestoperation *operation,RKMapPingResult *mapPingResult) { Location * location = (Location*)mapPingResult; self.ID = Location.ID; } failure:^(RKObjectRequestoperation *operation,NSError *error) { ALog(@"fail!"); }];总结
以上是内存溢出为你收集整理的ios – 带有查询参数的RestKit postObject全部内容,希望文章能够帮你解决ios – 带有查询参数的RestKit postObject所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)