zoukankan      html  css  js  c++  java
  • iOS网络篇

    iOS网络请求三步:

    1、新建URL连接

    2、新建请求(请求新建的URL连接)

    3、建立连接。

    然后就可以获取数据了。

    一、同步GET请求方法

    -(void)synchronizationGet
    
    {
    
        NSString *strURL = @"http://olasapi.sinaapp.com//index.php";
    
        NSURL *url = [NSURL URLWithString:strURL];
    
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    
        
    
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
        NSLog(@"请求完成");
    
        
    
        
    
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
        NSLog(@"%@",dic);
    
    }

    二、异步get

    -(void)asynchronousGet
    
    {
    
      
    
        NSString *strURL = @"http://olasapi.sinaapp.com//index.php";
    
        NSURL *url = [NSURL URLWithString:strURL];
    
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    
        
    
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
        
    
        if (connection) {
    
            _data = [NSMutableData new];
    
        }
    
    }
    
    #pragma mark - NSURLConnection回调方法
    
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    
    {
    
        [_data appendData:data];
    
    }
    
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    
    {
    
        
    
    }
    
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    
    {
    
        NSLog(@"请求完成");
    
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingAllowFragments error:nil];
    
        NSLog(@"%@",dic);
    
    }

    三、异步POST

    -(void)asynchronousPost
    
    {
    
        NSString *strURL = @"http://olasapi.sinaapp.com//index.php";
    
        NSURL *url = [NSURL URLWithString:strURL];
    
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    
        [request setHTTPMethod:@"POST"];
    
    //    [request setHTTPBody:@""]
    
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
        
    
        if (connection) {
    
            _data = [NSMutableData new];
    
        }
    
     
    
    }

    代理方法同get异步中的代理方法

  • 相关阅读:
    git删除目录,且保留本地的
    gitpush 免密码
    git常用操作
    ubuntu安装Nodejs
    ubuntu如何配置samba
    用AI将png转成svg做字符图标教程
    windows server 2012设置远程连接断开后自动注销
    windows 2012执行计划任务错误:操作员或系统管理员拒绝了请求(0x800710E0)
    删除节点
    代理 XP”组件已作为此服务器安全配置的一部分被关闭。系统管理员可以使用 sp_configure 来启用“代理 XP”。
  • 原文地址:https://www.cnblogs.com/zhanggui/p/4308079.html
Copyright © 2011-2022 走看看