2014-03-28 32 views
0

我有最奇怪的问题,试图从iOS设备与Django API进行通信,并POST一个NSDictionary,似乎无法使其工作。 好奇的是:相同的代码就像一个魅力与Ruby API交谈一样。AFNetworking 2.0无法发布到DJANGO(GET消息收到)

这是我的ObjC方法:

- (void)makePostToServerWithClass:(NSString *)stringClass object:(NSDictionary *)parametersDict success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure 

{ [[UIApplication的sharedApplication] setNetworkActivityIndi​​catorVisible: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 parametersDict

{ “raw_password”= 1234; username = testuser; }

在Linux服务器,Django的调试打印:

[28/MAR/2014 13时04分29秒] “GET/API/V1/create_user/HTTP/1.1” 405 4

而且我得到iOS上的错误是:

误差3:错误域= NSCocoaErrorDomain代码= 3840“的操作 无法完成。 (可可错误3840.)“(JSON文本没有启动 与数组或对象和选项允许片段未设置。) UserInfo = 0xb44e5b0 {NSDebugDescription = JSON文本不以 开始数组或对象和选项允许片段不。集, NSUnderlyingError = 0xb44e860 “请求失败:不允许的方法 (405)”}

最奇怪的是:我送一个POST和Linux服务器打印错误作为一个GET

你们有什么想法我做错了吗? 正如我之前说过的,它在Ruby API上运行良好。

+1

我认为,如果这段代码在带有ruby的服务器中工作,而不是在带有django的服务器中工作,那么服务器实现是不好的。 – HCarrasko

回答

0

以防万一有人在任何时候对这个同样的问题跌倒,因为我是用一种叫做RNCachingURLProtocol POD缓存检索到的图像问题被引起的,来自UIWebViews的HTML页面。 由于某种原因,此POD干扰了AFNetworking,不允许它发出POST请求。 一旦我删除了RNCachingURLProtocol,一切都像魅力一样。

0

你应该尝试从AFNetworking此功能: -

(AFHTTPRequestOperation *)POST:(NSString *)URLString 
         parameters:(NSDictionary *)parameters 
     constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block 
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success 
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure 
+0

请把你的代码放在''格式中。 – HCarrasko

+0

不幸的是它没有工作。 结果如下: [manager POST:@“http://192.168.2.4:8080/api/v1/create_user/”parameters:@ {@“username”:@“userTest”,@“密码“:@”123“}成功:^(AFHTTPRequestOperation *操作,id响应对象){ NSLog(@”SUCCESS“); (AFHTTPRequestOperation *操作,NSError *错误){ NSLog(@“FAILED:%@”,error.description); }]; – user3472897