ios NSURLRequest NSMutableURLRequest 数据请求

来源:互联网 发布:mac版金蝶友商智慧记 编辑:程序博客网 时间:2024/06/06 01:17

转自:http://www.maxiaoguo.com/clothes/256.html

get 请求

[objc] view plain copy
  1. #pragma mark - GET登录  
  2. - (void)getLogon  
  3. {  
  4.     // 1. URL  
  5.     NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@"self.userName.textself.userPwd.text];  
  6.       
  7.     NSURL *url = [NSURL URLWithString:urlStr];  
  8.       
  9.     // 2. Request  
  10.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  11.       
  12.     // 3. Connection  
  13.     // 1> 登录完成之前,不能做后续工作!  
  14.     // 2> 登录进行中,可以允许用户干点别的会更好!  
  15.     // 3> 让登录操作在其他线程中进行,就不会阻塞主线程的工作  
  16.     // 4> 结论:登陆也是异步访问,中间需要阻塞住  
  17.     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  
  18.           
  19.         if (connectionError == nil) {  
  20.             // 网络请求结束之后执行!  
  21.             // 将Data转换成字符串  
  22.             NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
  23.               
  24.             // num = 2  
  25.             NSLog(@"%@ %@", str, [NSThread currentThread]);  
  26.               
  27.             // 更新界面  
  28.             [[NSOperationQueue mainQueue] addOperationWithBlock:^{  
  29.                 self.logonResult.text = @"登录完成";  
  30.             }];  
  31.         }  
  32.     }];  
  33.       
  34.     // num = 1  
  35.     NSLog(@"come here %@", [NSThread currentThread]);  
  36.       
  37.     NSURLResponse *response = nil;  
  38.     // 1. &response真的理解了吗?  
  39.     // 2. error:为什么是NULL,而不是nil  
  40.     // NULL是C语言的 = 0  
  41.     // 在C语言中,如果将指针的地址指向0就不会有危险  
  42.       
  43.     // nil是OC的,是一个空对象发送消息不会出问题  
  44. //    [response MIMEType];  
  45.     [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];  
  46. }  

post请求

[objc] view plain copy
  1. #pragma mark - POST登录  
  2. - (void)postLogon  
  3. {  
  4.     // 1. URL  
  5.     NSURL *url = [NSURL URLWithString:@"http://localhost/login.php"];  
  6.       
  7.     // 2. 请求(可以改的请求)  
  8.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
  9.     // ? POST  
  10.     // 默认就是GET请求  
  11.     request.HTTPMethod = @"POST";  
  12.     // ? 数据体  
  13.     NSString *str = [NSString stringWithFormat:@"username=%@&password=%@"self.userName.textself.userPwd.text];  
  14.     // 将字符串转换成数据  
  15.     request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];  
  16.       
  17.     // 3. 连接,异步  
  18.     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  
  19.           
  20.         if (connectionError == nil) {  
  21.             // 网络请求结束之后执行!  
  22.             // 将Data转换成字符串  
  23.             NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
  24.               
  25.             // num = 2  
  26.             NSLog(@"%@ %@", str, [NSThread currentThread]);  
  27.               
  28.             // 更新界面  
  29.             [[NSOperationQueue mainQueue] addOperationWithBlock:^{  
  30.                 self.logonResult.text = str;  
  31.             }];  
  32.         }  
  33.     }];  
  34.       
  35.     // num = 1  
  36.     NSLog(@"come here %@", [NSThread currentThread]);  
  37. }  

使用NSURLConnection有两种方式: 第一种 如上, 第二种实现  NSURLConnectionDataDelegate 代理

[objc] view plain copy
  1. - (void)getLogon  
  2. {  
  3.     // 1. URL  
  4.     NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@"self.userName.textself.myPwd];  
  5.       
  6.     NSLog(@"%@"self.myPwd);  
  7.       
  8.     NSURL *url = [NSURL URLWithString:urlStr];  
  9.       
  10.     // 2. Request  
  11.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  12.       
  13.     // 3. 连接,已经10多岁了  
  14.     // 是一个很古老的技术  
  15.     NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];  
  16.       
  17.     // 开始工作,在很多多线程技术中,start run  
  18.     dispatch_async(dispatch_queue_create("demo", DISPATCH_QUEUE_CONCURRENT), ^{  
  19.         [connection start];  
  20.     });  
  21. }  
  22.   
  23. #pragma mark - NSURLConnectionDataDelegate代理方法  
  24. #pragma mark 接受到响应  
  25. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  
  26. {  
  27.     // 准备工作  
  28.     // 按钮点击就会有网络请求,为了避免重复开辟空间  
  29.     if (!self.data) {  
  30.         self.data = [NSMutableData data];  
  31.     } else {  
  32.         [self.data setData:nil];  
  33.     }  
  34. }  
  35.   
  36. #pragma mark 接收到数据,如果数据量大,例如视频,会被多次调用  
  37. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  
  38. {  
  39.     // 拼接数据,二进制流的体现位置  
  40.     [self.data appendData:data];  
  41. }  
  42.   
  43. #pragma mark 接收完成,做最终的处理工作  
  44. - (void)connectionDidFinishLoading:(NSURLConnection *)connection  
  45. {  
  46.     // 最终处理  
  47.     NSString *str = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];  
  48.       
  49.     NSLog(@"%@ %@", str, [NSThread currentThread]);  
  50. }  
  51.   
  52. #pragma mark 出错处理,网络的出错可能性非常高  
  53. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error  
  54. {  
  55.     NSLog(@"%@", error.localizedDescription);  
  56. }  
注: 更新UI都要在主线程更新,原因要保证线程安全

[objc] view plain copy
  1. // 更新界面  
  2.             [[NSOperationQueue mainQueue] addOperationWithBlock:^{  
  3.                 self.logonResult.text = str;  
  4.             }];  

0 0
原创粉丝点击