我在使用 RestKit 发布数据时遇到问题。当网络可用时,它实际上是在同步 Array DBMeetings 对象(离线创建)。 DBMeetings 对象可以有照片附件,所以我使用的是 RestKit 的以下方法。
multipartFormRequestWithObject:method:path:parameters:constructingBodyWithBlock:
问题是 multipartFormRequest 不能保留数据的 JSON 结构,这就是为什么我要创建一个 JSON 字符串并将其作为参数发布,以便服务器端可以适本地解析。我从遵循 SO 的回答 https://stackoverflow.com/a/19545138/1444321 中发现了这个想法
NSString *jsonString = [self jsonFromArray:unSyncedMeetings];
NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:nil
method:RKRequestMethodPOST
path:URL_MEETINGS_SYNC
parameters{@"total_files": [NSNumber numberWithInt:totalFiles], @"data": jsonString} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// just adding photo attachments in this block
}
如果我记录 jsonString ,它会像这样显示。
[{"id":92172,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-06T15:23:00Z","tags":[],"title":"abc","sample":false,"start_time":"2014-11-06T14:23:00Z","organizer":{"name":"Mithooo Popla","id":42900,"user_id":0,"email":"[email protected]"},"updated_at":"2014-11-06T14:25:34Z","meeting_topics":[{"id":800881,"title":"Tippih","meeting_id":92172,"type":"Topic","tags":[],"updated_at":"2014-11-06T14:25:34Z","created_at":"2014-11-06T14:23:47Z","children":[{"id":800882,"total_attachments":4,"topic_id":800881,"additional_owners":[],"title":"Tyhh\nVdh","tags":[],"type":"Note","attachments":[{"id":26291,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26291-IMG_0371-b100db1da64b5c001bafb629b32bfffaee000ecb.PNG?1415283828","file_name":"IMG_0371.PNG"},{"id":26289,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26289-IMG_0365-a7e2ee7f013b47d99a2ad06e95d7968676108f2b.PNG?1415283827","file_name":"IMG_0365.PNG"},{"id":26290,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26290-IMG_0364-c5ae106dd58ec4c31b3e0fc82721da1cee0444b7.JPG?1415283827","file_name":"IMG_0364.JPG"},{"id":26292,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26292-IMG_0366-0131a80b2199b6cc4d0d684f3acb78bc7a9c78a3.PNG?1415283828","file_name":"IMG_0366.PNG"}],"send_email":false,"updated_at":"2014-11-06T14:23:47Z","private":false,"created_at":"2014-11-06T14:23:47Z","user_id":42900,"deleted":false,"position":1,"unique_parent_id":"x-coredata:\/\/442A8ECF-97C6-493E-93D6-62BEBDEF5064\/DBTopics\/p2","status":0,"total_comments":0}],"presenter":{"user_id":0},"user_id":42900,"deleted":false,"subtopics":[],"parent_id":0,"position":1,"unique_id":"x-coredata:\/\/442A8ECF-97C6-493E-93D6-62BEBDEF5064\/DBTopics\/p2"}],"created_at":"2014-11-06T14:23:10Z","lock":false,"deleted":false,"required_participants":[]},{"id":91976,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-22T22:04:00Z","location":"","finish_date":"2014-11-22T22:04:00Z","sample":false,"start_time":"2014-11-22T21:04:00Z","title":"aaaaaa 25 we'd future date. hggggg","updated_at":"2014-10-31T13:47:46Z","tags":[],"created_at":"2014-10-31T13:47:45Z","lock":false,"organizer":{"name":"mr shake ","id":42902,"user_id":0,"email":"[email protected]"},"meeting_topics":[],"deleted":false,"required_participants":[{"name":"mr shake ","id":42902,"user_id":0,"email":"[email protected]"}],"start_date":"2014-11-22T21:04:00Z"},{"id":92187,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-07T14:04:00Z","location":"","finish_date":"2014-11-07T14:04:00Z","sample":false,"start_time":"2014-11-07T13:04:00Z","title":"hhhhhhhhhh ggggjdfd,f hhh","updated_at":"2014-11-07T15:00:14Z","tags":[],"created_at":"2014-11-07T13:04:12Z","lock":false,"organizer":{"name":"mr shake ","id":42902,"user_id":0,"email":"[email protected]"},"meeting_topics":[],"deleted":false,"required_participants":[{"name":"mr shake ","id":42902,"user_id":0,"email":"[email protected]"}],"start_date":"2014-11-07T13:04:00Z"},{"id":92185,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-07T12:49:37Z","location":"","finish_date":"2014-11-07T12:49:37Z","sample":false,"start_time":"2014-11-07T11:49:37Z","title":"jgfjfhtbn gggggggg","updated_at":"2014-11-07T11:49:37Z","tags":[],"created_at":"2014-11-07T11:49:37Z","lock":false,"organizer":{"name":"mr shake ","id":42902,"user_id":0,"email":"[email protected]"},"meeting_topics":[],"deleted":false,"required_participants":[{"name":"mr shake ","id":42902,"user_id":0,"email":"[email protected]"}],"start_date":"2014-11-07T11:49:37Z"}]
当我与 Charles 核对请求时,它仍然是正确的。这是查尔斯的截图。
但在服务器上,访问方式为
data="{\"meetings\":[{\"id\":91160,\"total_topics\":0,\"optional_participants\":[],\"readonly_participants\":[],\"finish_time\":\"2014-11-07T13:47:17Z\",\"location\":\"\",\"finish_date\":\"2014-11-07T13:47:17Z\",\"sample\":false,\"start_time\":\"2014-11-07T12:47:17Z\",\"title\":\"his 8898997998897897\",\"updated_at\":\"2014-11-07T12:47:27Z\",\"tags\":[],\"created_at\":\"2014-11-07T12:47:17Z\",\"lock\":false,\"organizer\":{\"name\":\"Rehmat Qadir\",\"id\":12891,\"user_id\":0,\"email\":\"[email protected]\"},\"meeting_topics\":[],\"deleted\":false,\"required_participants\":[{\"name\":\"Rehmat Qadir\",\"id\":12891,\"user_id\":0,\"email\":\"[email protected]\"}],\"start_date\":\"2014-11-07T12:47:17Z\"}]}"
反斜杠被添加到任何地方。服务器端无法将 data 的值视为 JSON 对象,无法解析数据。
知道实际问题是什么吗?任何帮助表示赞赏。
这就是我创建 jsonString 的方式
+ (NSString *)jsonFromArrayNSArray *)meetings {
NSArray *requestDescriptors = [[RKObjectManager sharedManager] requestDescriptors];
RKRequestDescriptor *requestDescriptorObject = [requestDescriptors objectAtIndex:10];
NSMutableDictionary *jsonDictionary = [[NSMutableDictionary alloc] init];
NSMutableArray *jsonArray = [[NSMutableArray alloc] init];
if (meetings.count > 0) {
for (DBMeetings *meeting in meetings) {
NSDictionary *parametersForObject = [RKObjectParameterization parametersWithObject:meeting requestDescriptor:requestDescriptorObject error:nil];
NSDictionary *meetingDictionary = [parametersForObject valueForKey"meetings"];
[jsonArray addObject:meetingDictionary];
}
}
[jsonDictionary setValue:jsonArray forKey"meetings"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonArray options:kNilOptions error:nil];
NSString *jsonString;
if(jsonData) {jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];}
return jsonString;
}
Best Answer-推荐答案 strong>
您创建的 JSON 字符串看起来不错,您发送它的方式和 Charles 日志也是如此。因此,这很可能是一个完全基于服务器的问题,涉及文本处理和内容转义。这通常应该是透明的,因此您需要展示它是如何处理的。更糟糕的是,您可以处理字符串并替换所有转义字符。
关于ios - RestKit 使用字典参数发布 multipartFormRequest 在数据中添加反斜杠,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/26806296/
|