【问题标题】:AFNetworking POST Writes to Server and Returns Failure Error (Cocoa Error 3840)AFNetworking POST 写入服务器并返回失败错误(Cocoa 错误 3840)
【发布时间】:2014-09-20 21:56:50
【问题描述】:

我一直在努力将一些 jpg 图像上传到使用 AFNetworking 2.0 的服务器以及我在 stackoverflow 上遇到的一些代码,但我收到了 Cocoa 错误 3840。我查了一下,发现一些关于txt 文件,但我没有上传 txt 文件,所以这让我很困惑。当我执行此代码时,它会开始向服务器写入一段时间,直到它因以下语句而崩溃:

Failure Error Domain=NSCocoaErrorDomain Code=3840 "操作 无法完成。 (Cocoa 错误 3840。)”(JSON 文本未开始 带有数组或对象以及允许未设置片段的选项。) UserInfo=0x155b1360 {NSDebugDescription=JSON 文本不是以 允许未设置片段的数组或对象和选项。}

我还是 AFNetworking 的新手,所以有人可以解释一下导致此错误的原因吗?

我在编写此代码时遇到的另一个问题是我正在尝试使用dataWithContentsOfFile 方法保存NSData,但在使用该方法后它一直返回nil。我尝试了多种方法,如下所示。当我为fileRoot 使用pathForResource_filename 包含扩展名)时,它返回nil,但另一种方法stringByAppendingPathComponent 按预期工作,但即使在将正确的路径作为@987654329 的参数之后@ 它仍然为 imageToUpload 返回 nil。现在我只是使用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];

【问题讨论】:

    标签: ios objective-c json cocoa-touch afnetworking


    【解决方案1】:

    我可以通过设置以下属性来解决上传失败问题:

    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    

    我还能够通过不使用 bundle 方法而是从我的文档目录中抓取来解决我的第二个问题,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2013-10-19
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 2013-09-02
      相关资源
      最近更新 更多