AFHTTPRequestOperationManager 如何从服务器获取响应
Posted
技术标签:
【中文标题】AFHTTPRequestOperationManager 如何从服务器获取响应【英文标题】:AFHTTPRequestOperationManager how to get response from server 【发布时间】:2013-10-30 17:26:26 【问题描述】:如何获得 JSON 响应?如果 php 打印一些 JSON 整个请求失败,如果 PHP 不打印任何内容,则响应成功。如何使用 AFHTTPRequestOperation ?
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @@"foo":@"barr";
UIImage *image = myImage.image;
[manager POST:@"http://local/index.php"
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
[formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.7)
name:@"file"
fileName:@"photo.jpg"
mimeType:@"image/jpeg"];
success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"Success: %@", responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error %@", operation.responseString);
];
【问题讨论】:
【参考方案1】:在成功块中尝试:
NSError *e;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[operation.responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&e];
【讨论】:
以上是关于AFHTTPRequestOperationManager 如何从服务器获取响应的主要内容,如果未能解决你的问题,请参考以下文章