AFNetworking 2.0 图片上传和 JSON 返回错误
Posted
技术标签:
【中文标题】AFNetworking 2.0 图片上传和 JSON 返回错误【英文标题】:AFNetworking 2.0 Picture Upload and JSON Return Error 【发布时间】:2013-12-21 21:38:34 【问题描述】:我的 AFNetworking 代码似乎没有将任何图像和文本上传到服务器,但是当我尝试将图像上传到 php 文件时,Json 返回下面列出的错误或错误代码 3840。任何想法都会是如果您需要更多信息,请告诉我。
-(void)upLoadImage
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://***.php"]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.5);
NSDictionary *parameters = @@"message": self.pictureMessage.text;
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:@"userfl" fileName:@"upload.jpg" mimeType:@"image/jpeg"];
success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
];
op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
PHP:
<?php
header('Content-type: application/json');
if(!empty($_POST))
$message = $_POST['message'];
$directory = $_SERVER['DOCUMENT_ROOT'] . '/appphotos';
$file = basename($_FILES['userfl']['upload.jpg']);
$uploadfile = $directory . $file;
echo 'message= '.$message;
var_dump($_FILES);
$randomPhotoID = md5(rand() * time());
echo json_encode($file);
if (move_uploaded_file($_FILES['userfl']['tmp_name'], $uploadfile))
$response["success"];
echo json_encode($response);
else
$response["failure"];
echo json_encode($response);
else
$response['Empty post data'];
echo json_encode($response);
?>
错误:
2013-12-23 01:06:45.720 Animal[13757:907] Success: message= array(1)
["userfl"]=>
array(5)
["name"]=>
string(10) "upload.jpg"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(14) "/tmp/phpeRVNwb"
["error"]=>
int(0)
["size"]=>
int(9613)
""null ***** <6d657373 6167653d 20617272 61792831 29207b0a 20205b22 75736572 666c225d 3d3e0a20 20617272 61792835 29207b0a 20202020 5b226e61 6d65225d 3d3e0a20 20202073 7472696e 67283130 29202275 706c6f61 642e6a70 67220a20 2020205b 22747970 65225d3d 3e0a2020 20207374 72696e67 28313029 2022696d 6167652f 6a706567 220a2020 20205b22 746d705f 6e616d65 225d3d3e 0a202020 20737472 696e6728 31342920 222f746d 702f7068 70655256 4e776222 0a202020 205b2265 72726f72 225d3d3e 0a202020 20696e74 2830290a 20202020 5b227369 7a65225d 3d3e0a20 20202069 6e742839 36313329 0a20207d 0a7d0a22 226e756c 6c>
【问题讨论】:
您能粘贴来自该 JSON 数据的响应吗?您是否设置了适当的标头作为响应? 看起来你的响应不是 json 应该是。你有什么建议吗? 是 - 将 PHP 响应编码为 JSON 数据 我做到了。我在标题中将它设置为 content/json。 【参考方案1】:试试这个?
manager.requestSerializer = [AFJSONRequestSerializer serializer];
【讨论】:
其实看看这个方法请:***.com/questions/25415469/…以上是关于AFNetworking 2.0 图片上传和 JSON 返回错误的主要内容,如果未能解决你的问题,请参考以下文章
尽管上传成功,但调用了 AFNetworking 2.0 POST + PHP 阻止失败
使用 AFNetworking 2.0 从 iOS 应用程序以流的形式上传 100 张图像
AFNetworking 2.0,使用 NSURLSession 下载图片