IOS网络(POST)

URL :  NSUrl  网址

Request : NSRequest  NSMutableRequest

connection: NSURLConnection

http://www.xxxxxxx/login.php?username= sasa & password = dsdsds;

request(请求),常用的方式有两种:GET,POST

GET请求方式的特点:请求体,(请求参数)直接拼接在URL之后

GET请求的特点:1,长度受限,最大为255字节,2,安全性差参数容易被别人看见,导致泄漏,尤其是重要的信息(比如用

户名,密码)。3,优点:可以直接用浏览器打开,方便测试

通常使用GET从服务器获取信息,并不会通过GET方式向服务器提交大量信息

请求:post get delete put

链接:同步   异步

同步链接:当前网络线程去做网络链接,链接过程中,线程一直处于被占用状态,次线程的其他任务执行会处于等待状态,网

络请求结束后,其他的任务依次执行,

通常情况下,同步连接会卡死主线程,导致页面元素无法被点击,产生假死现象

如果数据量小,且网速较快,同步连接是最佳选择

同步连接代码简单,只需要一个类方法即可,这个类方法接受完全部分的数据之后,将完整的数据作为返回值,返回给外界

NSURLResponse *response = nil;
NSError *error = nil;
    // 1 创建URL
NSURL *url = [NSURL URLWithString:@"http://172.16.1.123:8888/xpmz/joke/index.php/site/articlejson/?p=33"];
// 2 发送请求
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5]; // 3 获取数据
NSData *date = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:date options:NSJSONReadingMutableContainers error:nil]; NSLog(@"content:%@", [[dic objectForKey:@"arr"] objectForKey:@"content"]); NSLog(@"response: %@", response); NSLog(@"error: %@", error);


异步请求:有一个新的线程去做网络连接,当前线程并处于空闲状态,可以处理本线程的其他任务,不会出现所谓的“假死‘现象,

一般网速慢,传输数据量较大时,应选择异步联接,

异步链接的的缺点:异步链接是有其他线程去完成的,完成的时间随机性很大,业务逻辑需要做好处理

NSURL *url = [NSURL URLWithString:@"http://172.16.1.123:8888/xpmz/joke/index.php/site/articlejson/?p=33"];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
NSOperationQueue *quene = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:quene completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"asyn :%@", response);
        NSLog(@"date :%@", data);
        NSLog(@"connectionError:%@", connectionError);
    }];
post提交(同步)
 NSURLResponse *response = nil;
    NSError *error = nil;
    NSURL *url = [NSURL URLWithString:@"http://172.16.1.123:8888/xpmz/joke/index.php/site/loging"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
     NSData *data1 = [@"user_email=361131953@qq.com&user_pass=123456" dataUsingEncoding:NSUTF8StringEncoding];
    // 设置提交方式
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data1];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

post(异步提交)
NSURL *url = [NSURL URLWithString:@"http://172.16.1.123:8888/xpmz/joke/index.php/site/loging"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
    NSData *data1 = [@"user_email=361131953@qq.com&user_pass=123456" dataUsingEncoding:NSUTF8StringEncoding];
    // 设置提交方式
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data1];
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@", data);
        NSLog(@"%@", response);
        NSLog(@"%@", [dic objectForKey:@"msg"]);
    }];


 NSURL *url = [[NSURL alloc] initWithString:@"http://style.guofenmai.com/v1/api/style/hot?page=2"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSArray *list = [[dic objectForKey:@"content"] objectForKey:@"list"];
//        NSLog(@"%@",);
        
        for (NSDictionary *item in list) {
            NSLog(@"%@",item);
            Goods *goods = [[Goods alloc] init];
            [goods setValuesForKeysWithDictionary:item];
            [self.allGoods addObject:goods];
        }
        NSLog(@"%@",self.allGoods);
        [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        
    }];




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值