我正在尝试使用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]错误:错误 Domain = AFNetworkingErrorDomain Code = -1011“预期的状态代码 (200-299),获得400“UserInfo = 0x75c89e0 {NSLocalizedRecoverySuggestion = { “原因”:NULL, “类”: “java.lang.NumberFormatException”, “localizedMessage”: “空”, “消息”: “空”}, AFNetworkingOperationFailingURLRequestErrorKey = HTTP://creativemind.appspot.com/notifications> ;, NSErrorFailingURLKey = http://creativemind.appspot.com/notifications, NSLocalizedDescription =(200-299)中的预期状态代码,得到400, AFNetworkingOperationFailingURLResponseErrorKey =}
我认为重要的是要注意当我使用nil参数尝试相同的请求时,我得到相同的错误消息。我还尝试在同一个JSON上执行GET请求,以检查我是否可以访问它并且它完美地工作。我真的不知道我能做什么。任何帮助将不胜感激