【发布时间】:2014-03-28 16:17:02
【问题描述】:
我在尝试从 iOS 设备与 Django API 通信并发布 NSDictionary 时遇到了最奇怪的问题,但似乎无法使其正常工作。 奇怪的是:相同的代码就像与 Ruby API 对话的魅力。
这是我的 ObjC 方法:
- (void)makePostToServerWithClass:(NSString *)stringClass object:(NSDictionary *)parametersDict success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
__block NSError *errorMessage;
NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:[baseURLString stringByAppendingString:stringClass] parameters:parametersDict error:&errorMessage];
[request setTimeoutInterval:120];
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
success(operation, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", operation.responseString);
NSLog(@"ERROR2: %@", errorMessage.description);
NSLog(@"ERROR3: %@", error.description);
if(operation.responseObject)
errorMessage = [self creteErrorMessageForOperation:operation error:error];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
failure(operation, errorMessage);
}];
[manager.operationQueue addOperation:operation];
}
在该代码中,URLString 将是:
@"http://linuxServerIP:8080/api/v1/create_user/"
parametersDict 将是一个 NSDictionary,其中包含:
(lldb) po 参数字典
{ “原始密码” = 1234; 用户名 = 测试用户; }
在 Linux 服务器上,Django 调试打印:
[28/Mar/2014 13:04:29] "GET /api/v1/create_user/HTTP/1.1" 405 4
我在 iOS 上遇到的错误是:
ERROR3: Error Domain=NSCocoaErrorDomain Code=3840 "操作 无法完成。 (Cocoa 错误 3840。)”(JSON 文本未开始 带有数组或对象以及允许未设置片段的选项。) UserInfo=0xb44e5b0 {NSDebugDescription=JSON 文本不是以 允许未设置片段的数组或对象和选项。, NSUnderlyingError=0xb44e860 "请求失败:方法不允许 (405)"}
最奇怪的是:我正在发送一个 POST,Linux 服务器将错误打印为 GET。
你们知道我做错了什么吗? 正如我之前所说,它在 Ruby API 上运行良好。
【问题讨论】:
-
我认为,如果这段代码在带有 ruby 的服务器上工作,而不是在带有 django 的服务器上工作,那么服务器实现是不好的。
标签: ios objective-c django afnetworking-2