我用这个把头撞在墙上.我想UIImage从库中选择并将其上传到服务器,就像可以做到的那样<form action="http://blabla.request.cfm" method="post" enctype="multipart/form-data">.而不是成功我得到了这个错误:
error = Error Domain = NSCocoaErrorDomain Code = 3840"操作无法完成.(Cocoa error 3840.)"(JSON文本不是以数组或对象开头,而是选项允许未设置片段.)UserInfo = 0x145e5d90 {NSDebugDescription = JSON文本不以数组或对象和选项开头,以允许未设置片段.}
我试过这种方式:
-(void)uploadPhoto{
NSString *path = @"http://blabla.request.cfm";
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.9);
int priv = self.isPrivate ? 1 : 0;
NSDictionary *parameters = @{@"username": self.username, @"password" : self.password, @"private" : @(priv), @"photo" : imageData};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if(self.imageView.image){
[formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) …Run Code Online (Sandbox Code Playgroud) 按照本教程,我正在制作一个带有HTML请求的基本iPhone应用程序.
本教程让我在AFNetworking中使用AFJSONRequestOperation.问题是,我正在使用AFNetworking版本2,它不再具有AFJSONRequestOperation.
所以,当然,这个代码(从教程的大约一半,在" 查询iTunes Store搜索API " 标题下)不编译:
NSURL *url = [[NSURL alloc]
initWithString:
@"http://itunes.apple.com/search?term=harry&country=us&entity=movie"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"%@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
Run Code Online (Sandbox Code Playgroud)
我的问题是,我该如何替换AFJSONRequestOperation以便我可以继续使用AFNetworking 2.x?我用谷歌搜索了这一点,发现似乎没有其他人在问这个问题.
objective-c ios afnetworking afjsonrequestoperation afnetworking-2
我不明白为什么这么难.在线的所有教程和文章似乎都在谈论1.0 api,这是非常没用的.
我尝试了几种不同的方法,得到了不同的结果.我究竟做错了什么?
上传任务 - 这似乎不是使用多部分形式,wtf?
NSMutableURLRequest *request = [self.manager.requestSerializer multipartFormRequestWithMethod:@"POST"
URLString:[[NSURL URLWithString:url relativeToURL:[NSURL URLWithString:ApiBaseUrl]] absoluteString]
parameters:@{}
constructingBodyWithBlock:nil];
NSProgress *progress;
NSURLSessionUploadTask *task = [self.manager uploadTaskWithRequest:request
fromData:data
progress:&progress
completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"[error description] = %@", [error description]);
} else {
NSLog(@"success!");
}
}];
[task resume];
Run Code Online (Sandbox Code Playgroud)发布一个块 - 这似乎没有附加任何东西
[self.manager POST:url
parameters:@{}
constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
[formData appendPartWithFileData:data
name:@"post[picture]"
fileName:@"picture.jpg"
mimeType:@"image/jpeg"];
}
success:^(NSURLSessionDataTask *task, id response) {
NSLog(@"Success");
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Error: …Run Code Online (Sandbox Code Playgroud)之所以要求你这样做是因为我对params感到困惑.
据我所知,有一种方法可以使用多部分请求.
这种方式为我们提供了两个概念,因为我理解从文件上传,可以存储在Document目录中或使用NSData对象.
从文件上传:
所以我将test.jpg保存到文档目录中.然后我让NSURL实例在多部分请求中使用它.下面的代码显示了我如何创建NSURL实例:
NSString *fileName = @"test.jpg";
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSURL *filePath = [NSURL fileURLWithPath:folderPath];
Run Code Online (Sandbox Code Playgroud)
当我打印文件路径时:
file:///Users/mac/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/F732FCF6-EAEE-4E81-A88B-76ADB75EDFD1/Documents/test.jpg
Run Code Online (Sandbox Code Playgroud)
然后我设置我的参数并使用formData附加我的文件,如下所示:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
Run Code Online (Sandbox Code Playgroud)
它是否正确?或者我可能会错过一些东西因为回应不成功?
第二个概念 - 直接处理数据:
[formData appendPartWithFileData:imageData name:@"image" fileName:@"test.jpg" mimeType:@"image/jpeg"];
Run Code Online (Sandbox Code Playgroud)
但当应用程序调用这行代码时,我收到错误:
*** Terminating app due …Run Code Online (Sandbox Code Playgroud)