AFNetworking 2.0多部分/表格数据上传到mySQL

时间:2015-07-22 08:51:21

标签: php ios mysql afnetworking-2

我尝试过很多东西,但似乎无法让它发挥作用。我可以通过UITextField将文本上传到mySQL数据库,我可以单独通过UIImageView上传图像,但是当我尝试将它们组合在一起时它不起作用:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",nil];
NSDictionary *parameters = @{@"new": self.enter};
NSData *imageData = UIImageJPEGRepresentation(self.uploadImage.image, 0.5);
[manager POST:@"http://emily-ip.com/write.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFileData:imageData name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

此代码给出了错误:

"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=0x7fabeb081660 

所以我把它放进去了:

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

这会上传图片,但不会上传从文字字段输入的文字,这是我得到的输出:

Success: <4572726f 72>
Success: <53756363 65737366 756c>

我查看了很多帖子,但我找不到问题。无论如何,我认为它与文本字段有关。有人可以帮我修复代码吗?

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了。这是从文本字段和UIImageVIew发布图像和文本:

val fs:FileSystem = FileSystem.get(new URI(filePath), sc.hadoopConfiguration);
fs.delete(new Path(filePath), true) // true for recursive