1、AFNetWorking基本使用服務器
1 // 2 // ViewController.m 3 // IOS_0112_AFNetWorking 4 // 5 // Created by ma c on 16/2/11. 6 // Copyright © 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "AFNetworking.h" 11 12 @interface ViewController () 13 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 self.view.backgroundColor = [UIColor cyanColor]; 21 22 } 23 24 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 25 { 26 [self getData]; 27 } 28 29 - (void)postJSON 30 { 31 // 1.建立一個請求操做管理者 32 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; 33 // 2.聲明:等會服務器返回的是JSON數據,默認是JSON數據 34 mgr.responseSerializer = [AFJSONResponseSerializer serializer]; 35 36 // 2.請求參數 37 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 38 dict[@"username"] = @"123"; 39 dict[@"pwd"] = @"123"; 40 // 3.發送一個GET請求 41 NSString *url = @"http://localhost:8080/MJServer/login"; 42 [mgr POST:url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) { 43 ; 44 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 45 //判斷responseObject的類型 46 //NSLog(@"請求成功-----%@",[responseObject class]); 47 NSLog(@"請求成功-----%@", responseObject); 48 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 49 NSLog(@"請求失敗"); 50 }]; 51 52 53 } 54 55 - (void)getData 56 { 57 // 1.建立一個請求操做管理者 58 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; 59 // 2.聲明:不要對服務器返回的數據進行解析,直接返回data便可 60 mgr.responseSerializer = [AFHTTPResponseSerializer serializer]; 61 62 // 2.請求參數 63 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 64 dict[@"username"] = @"123"; 65 dict[@"pwd"] = @"123"; 66 //dict[@"type"] = @"XML"; 67 // 3.發送一個GET請求 68 NSString *url = @"http://localhost:8080/MJServer/login"; 69 [mgr GET:url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) { 70 ; 71 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 72 73 NSLog(@"請求成功-----%@", [responseObject class]); 74 75 NSDictionary *dcit = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]; 76 NSLog(@"%@",dict); 77 78 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 79 NSLog(@"請求失敗"); 80 }]; 81 82 83 } 84 - (void)getXML 85 { 86 // 1.建立一個請求操做管理者 87 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; 88 // 2.聲明:等會服務器返回的是XML數據 89 mgr.responseSerializer = [AFXMLParserResponseSerializer serializer]; 90 // 2.請求參數 91 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 92 dict[@"username"] = @"123"; 93 dict[@"pwd"] = @"123"; 94 dict[@"type"] = @"XML"; 95 // 3.發送一個GET請求 96 NSString *url = @"http://localhost:8080/MJServer/login"; 97 [mgr GET:url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) { 98 ; 99 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 100 101 NSLog(@"請求成功-----%@", responseObject); 102 //解析XML數據 103 104 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 105 NSLog(@"請求失敗"); 106 }]; 107 108 } 109 - (void)getJSON 110 { 111 // 1.建立一個請求操做管理者 112 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; 113 // 2.聲明:等會服務器返回的是JSON數據,默認是JSON數據 114 mgr.responseSerializer = [AFJSONResponseSerializer serializer]; 115 116 // 2.請求參數 117 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 118 dict[@"username"] = @"123"; 119 dict[@"pwd"] = @"123"; 120 // 3.發送一個GET請求 121 NSString *url = @"http://localhost:8080/MJServer/login"; 122 [mgr GET:url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) { 123 ; 124 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 125 //判斷responseObject的類型 126 //NSLog(@"請求成功-----%@",[responseObject class]); 127 NSLog(@"請求成功-----%@", responseObject); 128 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 129 NSLog(@"請求失敗"); 130 }]; 131 132 } 133 @end
2、AFNetWorking文件上傳app
1 // 2 // ViewController.m 3 // IOS_0112_AFNetWorking 4 // 5 // Created by ma c on 16/2/11. 6 // Copyright © 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "AFNetworking.h" 11 12 @interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate,UIActionSheetDelegate> 13 @property (weak, nonatomic) IBOutlet UIImageView *imageView; 14 15 - (IBAction)upload; 16 17 @end 18 19 @implementation ViewController 20 21 /* 22 文件下載,文件比較大時,斷點續傳技術:廣泛全部的HTTP服務器都支持 23 文件上傳,文件比較大時,斷點續傳技術:通常的HTTP服務器都不支持,經常使用的技術是Socket(TCP\IP,UDP) 24 */ 25 26 - (void)viewDidLoad { 27 [super viewDidLoad]; 28 self.view.backgroundColor = [UIColor cyanColor]; 29 30 } 31 32 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 33 { 34 [self uploadPhotos]; 35 } 36 37 - (void)uploadPhotos 38 { 39 UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"請選擇圖片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相冊", nil]; 40 [sheet showInView:self.view.window]; 41 42 // if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return; 43 // //上傳手機照片給服務器,圖片來源 44 // // 拍照:UIImagePickerControllerSourceTypeCamera 45 // // 相冊:UIImagePickerControllerSourceTypePhotoLibrary 46 // 47 // UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; 48 // //設置圖片來源 49 // //ipc.sourceType = UIImagePickerControllerSourceTypeCamera; 50 // ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 51 // //設置代理 52 // ipc.delegate = self; 53 // [self presentViewController:ipc animated:nil completion:nil]; 54 } 55 56 #pragma mark - UIActionSheetDelegate的代理方法 57 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 58 { 59 UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; 60 //設置代理 61 ipc.delegate = self; 62 63 switch (buttonIndex) { 64 case 0: //拍照 65 { 66 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return; 67 ipc.sourceType = UIImagePickerControllerSourceTypeCamera; 68 break; 69 } 70 case 1: //相片 71 { 72 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return; 73 ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 74 break; 75 } 76 77 default: 78 break; 79 } 80 //顯示控制器 81 [self presentViewController:ipc animated:nil completion:nil]; 82 } 83 84 #pragma mark - UIImagePickerControllerDelegate代理方法 85 //選擇完圖片後調用 86 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info 87 { 88 //銷燬控制器 89 [picker dismissViewControllerAnimated:nil completion:nil]; 90 //得到圖片 91 UIImage *image = info[UIImagePickerControllerOriginalImage]; 92 //顯示圖片 93 self.imageView.image = image; 94 NSLog(@"%@",image); 95 96 } 97 98 - (IBAction)upload { 99 if (self.imageView.image == nil) return; 100 101 // 1.建立一個請求操做的管理者 102 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; 103 // 2.封裝參數(此參數只能放非文件參數) 104 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 105 dict[@"username"] = @"bowen"; 106 dict[@"pwd"] = @"123"; 107 108 // 2.發送一個請求 109 NSString *url = @"http://localhost:8080/MJServer/upload"; 110 111 [mgr POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 112 113 /* 114 FileData:須要上傳文件的具體數據 115 name:服務器那邊接收文件用的參數名 116 fileName:告訴服務器所上傳文件的文件名 117 mimeType:所上傳文件的文件類型 118 */ 119 120 NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0); 121 [formData appendPartWithFileData:fileData name:@"file" fileName:@"pic.jpg" mimeType:@"image/jpeg"]; 122 123 } progress:^(NSProgress * _Nonnull uploadProgress) { 124 ; 125 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 126 NSLog(@"上傳成功"); 127 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 128 NSLog(@"上傳失敗"); 129 }]; 130 131 } 132 133 134 135 #pragma mark - uploadFile 136 - (void)uploadFile 137 { 138 // 1.建立一個請求操做的管理者 139 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; 140 // 2.封裝參數(此參數只能放非文件參數) 141 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 142 dict[@"username"] = @"bowen"; 143 dict[@"pwd"] = @"123"; 144 145 // 2.發送一個請求 146 NSString *url = @"http://localhost:8080/MJServer/upload"; 147 148 [mgr POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 149 //在發送請求以前會自動調用這個block 150 //須要在這個block中添加文件參數到formData 151 152 /* 153 FileURL:須要上傳的文件的URL路徑 154 name:告訴服務器那邊接收文件用的參數名 155 fileName:告訴服務器所上傳文件的文件名 156 mimeType:所上傳文件的文件類型 157 */ 158 NSURL *url = [[NSBundle mainBundle] URLForResource:@"abc" withExtension:@"jpg"]; 159 [formData appendPartWithFileURL:url name:@"file" fileName:@"text.jpg" mimeType:@"image/jpeg"error:nil]; 160 161 /* 162 FileData:須要上傳文件的具體數據 163 name:服務器那邊接收文件用的參數名 164 fileName:告訴服務器所上傳文件的文件名 165 mimeType:所上傳文件的文件類型 166 167 UIImage *image = [UIImage imageNamed:@"abc"]; 168 NSData *fileData = UIImageJPEGRepresentation(image, 1); 169 [formData appendPartWithFileData:fileData name:@"file" fileName:@"pic.jpg" mimeType:@"image/jpeg"]; 170 171 */ 172 173 } progress:^(NSProgress * _Nonnull uploadProgress) { 174 ; 175 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 176 NSLog(@"上傳成功"); 177 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 178 NSLog(@"上傳失敗"); 179 }]; 180 } 181 182 @end