2014-09-20 99 views
1

我一直在努力獲取一些jpg圖像上傳到服務器與AFNetworking 2.0和一些代碼我遇到了stackoverflow,但我已經越來越可可錯誤3840.我看了這個,發現有關txt文件的一些信息,但我沒有上傳txt文件,所以這讓我感到困惑。當我執行這個代碼就開始寫服務器一段時間,直到它與下面的語句崩潰:AFNetworking POST寫入服務器並返回失敗錯誤(Cocoa錯誤3840)

失敗錯誤域= NSCocoaErrorDomain代碼= 3840「的操作 無法完成(可可錯誤3840 。「)(JSON文本沒有啓動 與數組或對象和選項允許片段未設置。) UserInfo = 0x155b1360 {NSDebugDescription = JSON文本沒有開始 數組或對象和選項允許片段不設置。}

我還是AFNetworking的新手,所以有人可以解釋一下是什麼原因這個錯誤?

我在編寫此代碼時遇到的另一個問題是,我試圖使用dataWithContentsOfFile方法保存NSData,但在使用該方法後它會一直返回nil。我已經嘗試了多種方式,它們在下面進行了編碼。當我使用pathForResource_filename包含擴展)爲fileRoot返回nil,但如預期stringByAppendingPathComponent工作的另一種方法,但後來甚至具有dataWithContentsOfFile正確的路徑作爲參數後,仍返回nilimageToUpload。現在我剛剛用UIImageJPEGRepresentation解決了它的問題。

有人也可以請解釋可能是什麼原因造成的?

對不起,如果我不應該在一篇文章中發佈2個單獨的問題。我的第一個問題是最緊迫的。謝謝。

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys: 
           _username, @"username", 
           _filename, @"filename", 
           _metadata, @"metadata", 
           nil]; 

    // 1. Create `AFHTTPRequestSerializer` which will create your request. 
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer]; 

    //NSString* fileRoot = [[NSBundle mainBundle] pathForResource:_filename ofType:nil]; 
    //NSString *fileRoot=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_filename]; 
    //NSData *imageToUpload = [NSData dataWithContentsOfFile:fileRoot]; 
    NSData *imageToUpload = UIImageJPEGRepresentation(_savedFiles[_selectedRowNumber], 1.0); 

    // 2. Create an `NSMutableURLRequest`. 
    NSMutableURLRequest *request = 
    [serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://69.91.198.44:8080/GeodatabaseServer/FileUpload" 
            parameters:parameters 
        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
         [formData appendPartWithFileData:imageToUpload 
                name:_filenameNoExt 
               fileName:_filename 
               mimeType:@"image/jpeg"]; 
        }error:nil]; 

    // 3. Create and use `AFHTTPRequestOperationManager` to create an `AFHTTPRequestOperation` from the `NSMutableURLRequest` that we just created. 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    AFHTTPRequestOperation *operation = 
    [manager HTTPRequestOperationWithRequest:request 
            success:^(AFHTTPRequestOperation *operation, id responseObject) { 
             NSLog(@"Success %@", responseObject); 
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
             NSLog(@"Failure %@", error.description); 
            }]; 

    // 4. Set the progress block of the operation. 
    [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten, 
             long long totalBytesWritten, 
             long long totalBytesExpectedToWrite) { 
     NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite); 
    }]; 

    // 5. Begin! 
    [operation start]; 

回答

1

我能修復故障通過設置以下屬性上傳:

manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments]; 

我也能夠通過不使用捆綁的方法來解決我的第二個問題,而不是從我的文檔抓取目錄如下:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:_filename]; 
相關問題