UI_网络编程
来源:互联网 发布:关于初中英语的软件 编辑:程序博客网 时间:2024/06/05 06:55
- 什么是网络编程?
为什么要学习网络编程?
网络编程主要做什么事情?
什么是C/S模式?什么又是B/S模式?跟我们有什么关系?
client server
browser server对于我们来说,网络编程难学吗?
在百度输入一个文字搜索后,都做了哪些事情?
// 119.75.217.109
6.1 打开浏览器,输入百度的地址,按下回车
6.2 获取到了数据,浏览器解析数据,显示出来
6.3 输入框输入要搜索的内容:iPhone
6.4 浏览器通过地址,把要搜索的数据发送给了百度的服务器
6.5 服务器根据条件去给你查询数据,之后将数据返回给浏览器
6.6 浏览器又解析数据,并显示请求对象、链接对象都是什么?
请求:GET、POST
链接:同步、异步
GET 同步
GET 异步 Block方式
POST 同步
POST 异步 Delegate实现
请求对象: NSMutableURLRequest
setHTTPMethod:
setHTTPBody:
链接对象: NSURLConnection
sendSync…….
sendAsync…..
connection…..
如果需要数据
0. 准备NSURL
1. 创建请求对象
设置请求方式:GET、POST
如果选择了POST方式,设置请求数据
2. 创建链接对象并发送请求
使用同步方式或者异步方式
//#pragma mark - GET同步- (IBAction)getSyncButtonAction:(UIButton *)sender{ // 1. 准备URL NSURL *url = [NSURL URLWithString:BASE_URL_1]; // 2. 创建请求对象(参数是地址) NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"GET"]; // 设置请求方式 // 3. 创建链接对象并发送请求 // 3.1 保存结果附加信息的对象 NSURLResponse *respone = nil; // 3.2 保存错误信息的对象 NSError *error = nil; // 3.3 发送请求,并接受结果 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&respone error:&error]; // 4. 打印 NSLog(@"%lld", respone.expectedContentLength); NSLog(@"%@", respone.URL); // 打印错误 NSLog(@"%@", error); // 解析数据,并显示到页面上 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; _textView.text = [NSString stringWithFormat:@"%@", dict];}//#pragma mark - GET异步- (IBAction)getAsyncButtonAction:(UIButton *)sender{ // 1. 准备URL NSURL *url = [NSURL URLWithString:BASE_URL_1]; // 2. 创建请求对象(参数是地址) NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"GET"]; // 设置请求方式 // 3. 创建链接对象,发送请求 __block ViewController *weakSelf = self; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 4. 解析数据 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; // 5. 使用当前控制器的属性的时候,不能直接写属性名称,照样需要__block修饰的变量去执行 weakSelf.textView.text = [NSString stringWithFormat:@"%@", dict]; }];}//#pragma mark - POST同步- (IBAction)postSyncButtonAction:(UIButton *)sender{ // 1. 准备网址 NSURL *url = [NSURL URLWithString:BASE_URL_2]; // 2. 准备参数(直接给字符串发送dataUsingEncoding消息,使用UTF-8编码) NSData *paramData = [BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding]; // 3. 创建请求对象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; // 设置请求方式 [request setHTTPBody:paramData]; // 设置参数 // 4. 创建链接对象,并发送请求 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; // 5. 解析并显示 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; _textView.text = [NSString stringWithFormat:@"%@", dict];}//#pragma mark - POST异步- (IBAction)postAsyncButtonAction:(UIButton *)sender{ // 1. 准备网址 NSURL *url = [NSURL URLWithString:BASE_URL_2]; // 2. 准备参数(直接给字符串发送dataUsingEncoding消息,使用UTF-8编码) NSData *paramData = [BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding]; // 3. 创建请求对象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; // 设置请求方式 [request setHTTPBody:paramData]; // 设置参数 // 4. 创建链接对象,并发送请求 [NSURLConnection connectionWithRequest:request delegate:self];}//#pragma mark - NSURLConnectionDataDelegate//#pragma mark 开始接受请求结果- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ self.allData = [NSMutableData data];}//#pragma mark 开始接受数据- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [_allData appendData:data];}///#pragma mark 数据接收完毕- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_allData options:NSJSONReadingAllowFragments error:nil]; _textView.text = [NSString stringWithFormat:@"%@", dict];}//#pragma mark 发生错误,执行此方法- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"%@", error);}//#pragma mark - 清除- (IBAction)clearButtonAction:(UIButton *)sender { _textView.text = nil;}
0 0
- UI_网络编程
- UI_网络编程
- UI_多线程编程
- UI_组件
- UI_多线程
- UI_协议
- UI_手势
- UI_手势
- UI_霓虹灯
- UI_动画
- UI_通讯录
- UI_数据库
- UI_封装
- UI_手势
- UI_涂鸦作业
- UI_自定义视图
- UI_本地存储
- UI_基本控件_UILabel_UITextField_UIButton_UIFont_UIAlertView
- 多线程
- Spring注解
- radminpass.php
- 读写properties文件
- 序列化和反序列化
- UI_网络编程
- 方便测试的 adb 命令 集合
- leetcode-118 Pascal's Triangle
- Android 在程序中关闭和打开屏幕
- 使用科大讯飞的语音技术实现语音识别
- android - anim translate中 fromXDelta、toXDelta、fromYDelta、toXDelta属性
- 2015年4月7号的日志
- CentOS7 或 RHEL7 mount windows共享目录 Permission denied 问题
- CentOS连接OpenVPN教程(Linux下OpenVPN客户端配置教程)