zoukankan      html  css  js  c++  java
  • NSURLConnection获取数据

    - (void)loadDataFromUrl
    {
        NSURL* url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101190408.html"];
        NSMutableURLRequest * urlRequest=[NSMutableURLRequest requestWithURL:url];
        NSURLConnection* urlConn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
        [urlConn start];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
    {
        NSHTTPURLResponse* rsp = (NSHTTPURLResponse*)response;
        int code = [rsp statusCode];
        if (code != 200)
        {
            [connection cancel];
            [connection release];
            connection = nil;
        }
        else
        {
            if (mData != nil)
            {
                [mData release];
                mData = nil;
            }
            mData = [[NSMutableData alloc] init];
        }
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [mData appendData:data];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
       // [self hideAlert];
        NSString* backString = [[NSMutableString alloc] initWithData:mData encoding:NSUTF8StringEncoding];
        NSMutableDictionary *backData  =[backString JSONValue];
        NSLog(@"%@",backData);
        connection = nil;
    }
    
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        [connection release];
        connection = nil;
    }
    
    -(void)showAlertView:(NSString*)titleStr
    {
        UIAlertView *myalert = [[UIAlertView alloc]
                                initWithTitle:@"提示"
                                message:titleStr
                                delegate:self
                                cancelButtonTitle:@"取消"
                                otherButtonTitles:nil];
        [myalert show];
        [myalert release];
    }
  • 相关阅读:
    2020-2021-1 20201221 《信息安全专业导论》第五周学习总结
    XOR加密
    2020-2021-1 20201221 《信息安全专业导论》第四周学习总结
    [SQL]创建数据库
    [SQL]基本表的定义及其完整性约束
    [SQL]修改和删除基本表
    [SQL]连接查询
    [SQL]嵌套查询
    机器学习中常用的求导公式
    [C++]-unordered_map 映射
  • 原文地址:https://www.cnblogs.com/jiackyan/p/3402159.html
Copyright © 2011-2022 走看看