AFNetWorkingSession
由于iOS9废弃了NSURLConnection,所以,原本的AFNetWorking里的hettrequestmanager这个应该是不行了,虽然依然可以用,但里边仍然含有已经废弃的方法,所以这里总结一下session的用法
请求数据
//启动一个sessionManager
AFURLSessionManager * dataManager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//设置响应器,有三种,data ,xml JSON,这里用data 默认时JSON
dataManager.responseSerializer = [AFHTTPResponseSerializer serializer];
//初始化请求
NSURL * dataURL = [NSURL URLWithString:@"http://10.0.8.8/sns/my/user_list.php"];
NSURLRequest * dataRequest = [NSURLRequest requestWithURL:dataURL];
//请求数据
NSURLSessionDataTask * dataTask = [dataManager dataTaskWithRequest:dataRequest completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dict);
}];
[dataTask resume];
上传数据
这里说明一下,如果是上传数据的话,这种方式要用NSMutableURLRequest类来生命request,不然会出现各种错误,原作者展示的是url的上传,这个我也不是很清楚,然这样做的话,就不报错了….很奇怪
AFURLSessionManager * uploadManager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
uploadManager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURL * uplpadURL = [NSURL URLWithString:@"http://10.0.8.8/sns/my/upload_headimage.php"];
NSMutableURLRequest * uploadRequest = [NSMutableURLRequest requestWithURL:uplpadURL];
UIImage * uploadImage = [UIImage imageNamed:@"10_7.jpg"];
NSData * uploadData = UIImageJPEGRepresentation(uploadImage, 1.0);
uploadRequest.HTTPMethod = @"POST";
NSURLSessionUploadTask * uploadTask = [uploadManager uploadTaskWithRequest:uploadRequest fromData:uploadData progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dict);
} ];
[uploadTask resume];
另一种上传数据
完全按照官方GitHub上写的,可以上传任意数据
UIImage * uploadMutableImage = [UIImage imageNamed:@"10_7.jpg"];
NSData * uploadMutableData = UIImageJPEGRepresentation(uploadMutableImage, 1.0);
AFURLSessionManager * uploadMutableManager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
uploadMutableManager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableURLRequest * uploadMutableRequest = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://10.0.8.8/sns/my/upload_headimage.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:uploadMutableData name:@"headimage" fileName:@"10_7" mimeType:@"iamge/jpeg"];
} error:nil];
NSURLSessionUploadTask * uploadMutableTask = [uploadMutableManager uploadTaskWithStreamedRequest:uploadMutableRequest progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"mutableUpLoad : %@",dict);
}];
[uploadMutableTask resume];