ios - 使用 AFNetworking、ios 上传图片

标签 ios afnetworking-2

我必须通过发送照片字节将图像上传到服务器,并且我正在使用 AFNetworking,
但我不断收到这个错误-

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x10abb7350 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

最佳答案

如果 PHP 服务器没有响应 application/json ,那么你必须告诉你的manager接受一般 HTTP 响应:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:@"image" fileName:imageFilename mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"Success: %@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

如果您的服务器响应 JSON 响应,那么它可能没有设置 Content-Type header 正确,例如 header("Content-type: application/json"); .

关于ios - 使用 AFNetworking、ios 上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19966941/

相关文章:

ios - NSUserDefaults 中的 Web API 客户端单例与 token

iphone - Twitter 和 FB 集成到 iPhone 应用程序

ios - 将 NSValue 转换回存储在其中的结构类型?

ios - Collection View reloadData 方法显示选中的单元格

objective-c - 从 iPhone 应用程序编辑我的应用程序 settings.bundle 的设置

c# - 在 iOS 上引发的 Xamarin.Forms.WebView.Navigating 事件用于内部导航

objective-c - 使用 AFNetworking 发布请求 Amazon S3

ios - iOS未授权的网络连接(401)

iphone - 如何使用 AFNetworking 2.0 下载图像?

ios - `AFNetworking` 需要 CocoaPods `Podfile`