1 #import "HMViewController.h" 2 #import "MBProgressHUD+MJ.h" 3 4 @interface HMViewController () <NSURLConnectionDataDelegate> 5 @property (weak, nonatomic) IBOutlet UITextField *usernameField; 6 @property (weak, nonatomic) IBOutlet UITextField *pwdField; 7 - (IBAction)login; 8 9 /** 10 * 用来存放服务器返回的所有数据 11 */ 12 @property (nonatomic, strong) NSMutableData *responseData; 13 @end 14 15 @implementation HMViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 21 } 22 23 /** 24 * 登录逻辑 25 */ 26 - (IBAction)login 27 { 28 // 1.表单验证(输入验证) 29 NSString *username = self.usernameField.text; 30 if (username.length == 0) { // 没有输入用户名 31 [MBProgressHUD showError:@"请输入用户名"]; 32 return; 33 } 34 35 NSString *pwd = self.pwdField.text; 36 if (pwd.length == 0) { // 没有输入密码 37 [MBProgressHUD showError:@"请输入密码"]; 38 return; 39 } 40 41 // 弹框 42 [MBProgressHUD showMessage:@"正在拼命登录中..."]; 43 44 // 2.发送请求给服务器(带上帐号和密码) 45 // GET请求:请求行\请求头 46 47 // 2.1.设置请求路径 48 NSString *urlStr = [NSString stringWithFormat:@"http://192.168.1.200:8080/MJServer/login?username=%@&pwd=%@", username, pwd]; 49 50 // 转码 51 urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 52 53 // URL里面不能包含中文 54 NSURL *url = [NSURL URLWithString:urlStr]; 55 56 // 2.2.创建请求对象 57 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 默认就是GET请求 58 request.timeoutInterval = 5; // 设置请求超时 59 60 // 2.3.发送请求 61 [self sendAsync:request]; 62 63 NSLog(@"---------已经发出请求"); 64 } 65 66 #pragma mark - NSURLConnectionDataDelegate 代理方法 67 /** 68 * 请求错误(失败)的时候调用(请求超时\断网\没有网, 一般指客户端错误) 69 */ 70 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 71 { 72 NSLog(@"connection:didFailWithError:"); 73 [MBProgressHUD hideHUD]; 74 75 [MBProgressHUD showError:@"网络繁忙, 请稍后再试"]; 76 } 77 78 /** 79 * 当接受到服务器的响应(连通了服务器)就会调用 80 */ 81 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 82 { 83 NSLog(@"connection:didReceiveResponse:"); 84 85 // 初始化数据 86 self.responseData = [NSMutableData data]; 87 } 88 89 /** 90 * 当接受到服务器的数据就会调用(可能会被调用多次, 每次调用只会传递部分数据) 91 */ 92 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 93 { 94 NSLog(@"connection:didReceiveData:"); 95 96 // 拼接(收集)数据 97 [self.responseData appendData:data]; 98 } 99 100 /** 101 * 当服务器的数据接受完毕后就会调用 102 */ 103 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 104 { 105 NSLog(@"connectionDidFinishLoading:"); 106 107 // 隐藏HUD 108 [MBProgressHUD hideHUD]; 109 110 // 解析服务器返回的数据 111 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:nil]; 112 NSString *error = dict[@"error"]; 113 if (error) { // 登录失败 114 [MBProgressHUD showError:error]; 115 } else { // 登录成功 116 NSString *success = dict[@"success"]; 117 [MBProgressHUD showSuccess:success]; 118 } 119 } 120 121 /** 122 * 发送异步请求的方式2: start方法, 代理 123 */ 124 - (void)sendAsync2:(NSURLRequest *)request 125 { 126 NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self]; 127 [conn start]; // 异步执行 128 } 129 130 /** 131 * 发送异步请求的方式1: 类方法, block 132 */ 133 - (void)sendAsync:(NSURLRequest *)request 134 { 135 NSOperationQueue *queue = [NSOperationQueue mainQueue]; 136 [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 当请求结束的时候调用 (拿到了服务器的数据, 请求失败) 137 138 // 隐藏HUD (刷新UI界面, 一定要放在主线程, 不能放在子线程) 139 [MBProgressHUD hideHUD]; 140 141 /** 142 解析data : 143 {"error":"用户名不存在"} 144 {"error":"密码不正确"} 145 {"success":"登录成功"} 146 */ 147 if (data) { // 请求成功 148 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 149 NSString *error = dict[@"error"]; 150 if (error) { // 登录失败 151 [MBProgressHUD showError:error]; 152 } else { // 登录成功 153 NSString *success = dict[@"success"]; 154 [MBProgressHUD showSuccess:success]; 155 } 156 } else { // 请求失败 157 [MBProgressHUD showError:@"网络繁忙, 请稍后再试"]; 158 } 159 }]; 160 } 161 162 @end