我使用以下代码将图像上传到服务器。
NSString *url = [NSString stringWithFormat:@"http://192.250.1.52:xxx/api/user/profileUploadUser?userid=27&emailid=tom@gnts.in"];
NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc]init];
[jsonDict setObject:ImageToUpload forKey:@"file"];
////////ImageToUpload = /var/mobile/Containers/Data/Application/89112E0B-C1D6-4408-8586-6C5B4A431713/Documents/61.png
[manager POST:url parameters:jsonDict
success:^(AFHTTPRequestOperation *operation, id responseObject)
{ //////success code }
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Image upload" message:@"failure" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
NSLog(@"error message %@",error);
// handle failure
}];
它总是进入故障区域 显示错误消息。
错误消息:错误Domain = NSURLErrorDomain Code = -1001"请求 超时。" UserInfo = {NSErrorFailingURLStringKey = http://192.250.1.52:xxxx/api/user/profileUploadUser?userid=27&emailid=tom@gnts.in,_ kCFStreamErrorCodeKey = -21022}}}
答案 0 :(得分:0)
检查您的网速可能会很慢,
[manager.requestSerializer setTimeoutInterval:30];//Set request timeout interval time & interact with backend developer for same.
您还可以在retry counter
和singleton
中管理一个public static Book book=null;
public static Book getBook(){
if (book==null){
book=new Book();
return book;
}else {
return book;
}
}
。如果失败则给出第二个请求&如果再次失败然后显示吐司消息互联网速度很慢。
答案 1 :(得分:0)
您正在尝试POST
操作,而您想要的是file upload
。
尝试 AFNetworking multipart 文件上传。