zoukankan      html  css  js  c++  java
  • 使用NSConnection下载数据

    1.创建NSConnection对象,设置委托对象
        
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self urlString]]];
        [NSURLConnection connectionWithRequest:request delegate:self];
        
        2. NSURLConnection delegate委托方法
            - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;  
            - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;  
            - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;  
            - (void)connectionDidFinishLoading:(NSURLConnection *)connection;  
    
        3. 实现委托方法
        - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
            // store data
            [self.receivedData setLength:0];            //通常在这里先清空接受数据的缓存
        }
        
        - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
               /* appends the new data to the received data */
            [self.receivedData appendData:data];        //可能多次收到数据,把新的数据添加在现有数据最后
        }
    
        - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
            // 错误处理
        }
    
        - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
            // disconnect
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;   
            NSString *returnString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
            NSLog(returnString);
            [self urlLoaded:[self urlString] data:self.receivedData];
            firstTimeDownloaded = YES;
        }
    
    
  • 相关阅读:
    第一篇阅读笔记
    课程信息管理系统
    HDU1124求n的阶乘后0的个数
    分解质因数算法
    牛客小白月赛23 B阶乘(质因数分解)
    JAVAWEB将图片铺满整个页面的方法
    Codeforces Global Round 7
    POJ--1703并查集(区分两个集合)
    POJ--1611经典并查集
    DFS,BFS回顾(各种题)(肺炎疫情中,祝平安)
  • 原文地址:https://www.cnblogs.com/jiewong/p/1925454.html
Copyright © 2011-2022 走看看