我正在尝试使用 AFNetworking 发出 PUT 请求。远程位置有一个 JSON,我想用请求更新它。
我已经设法将我的对象序列化为 JSON,并将其放入 NSDictionary 对象:
NSDictionary *container = [NSDictionary dictionaryWithObject:[self notifications] forKey:@"notifications"];
这样,当我使用 NSLog 打印出容器时,我得到的正是我想要发送的 JSON。
然后我尝试使用 AFNetworking 来满足我的请求:
AFHTTPClient *putClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://creativemind.appspot.com"]];
[putClient setParameterEncoding:AFJSONParameterEncoding];
NSMutableURLRequest *putRequest = [putClient requestWithMethod:@"PUT"
path:@"/notifications"
parameters:container];
AFHTTPRequestOperation *putOperation = [[AFHTTPRequestOperation alloc] initWithRequest:putRequest];
[putClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[putOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[putOperation start];
当我尝试此解决方案时,我收到以下错误消息:
2013-08-10 16:10:40.741 NotificationReader [1132:c07] 错误:错误域 = AFNetworkingErrorDomain 代码 = -1011“(200-299)中的预期状态代码,得到 400”用户信息 = 0x75c89e0 {NSLocalizedRecoverySuggestion = {“原因” :null,"class":"java.lang.NumberFormatException","localizedMessage":"null","message":"null"}, AFNetworkingOperationFailingURLRequestErrorKey=http://creativemind.appspot.com/notifications>, NSErrorFailingURLKey= http ://creativemind.appspot.com/notifications , NSLocalizedDescription=(200-299) 中的预期状态码, 得到 400, AFNetworkingOperationFailingURLResponseErrorKey=}
我认为重要的是要注意,当我使用 nil 参数尝试相同的请求时,我会收到相同的错误消息。我还尝试对同一个 JSON 执行 GET 请求,以检查我是否可以访问它并且它工作正常。我真的不知道我能做什么。任何帮助,将不胜感激