2014-03-28 27 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