当前位置:编程学习 > wap >>

IOS学习十一:ios开发之网络编程

这边我就以最简单的例子来引入几个常用的API中的类。
[cpp] 
// 
//  NLViewController.m 
//  NetWorkTest 
// 
//  Created by Nono on 12-5-16. 
//  Copyright (c) 2012年 NonoWithLilith. All rights reserved. 
// 
#import "NLViewController.h" 
 
@interface NLViewController () 
 
@end 
 
@implementation NLViewController 
@synthesize label = _label; 
@synthesize data = _data; 
@synthesize connection = _connection; 
- (void)dealloc{ 
    [self.label release]; 
    [self.data release]; 
    [super dealloc]; 

- (void)viewDidLoad 

    [super viewDidLoad]; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 300.0, 400)]; 
    self.label = label; 
    label.textAlignment = UITextAlignmentCenter; 
    [label setNumberOfLines:0]; 
    label.lineBreakMode = UILineBreakModeWordWrap;  
    self.label.text = @"正在在请求数据"; 
    [self.view addSubview:label]; 
    [label release]; 
    //step 1:请求地址 
    NSString *urlString = @"www.zzzyk.com"; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    //step 2:实例化一个request 
    NSURLRequest *requrst = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; 
    //step 3:创建链接 
    self.connection = [[NSURLConnection alloc] initWithRequest:requrst delegate:self]; 
    if ( self.connection) { 
        NSLog(@"链接成功"); 
    }else { 
        NSLog(@"链接失败"); 
    } 
     
    [url release]; 
    [urlString release]; 
    [requrst release]; 
    // Do any additional setup after loading the view, typically from a nib. 

 
- (void)viewDidUnload 

    self.label = nil; 
    self.data = nil; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 

 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
#pragma mark- 
#pragma NSUrlConnectionDelegate methods 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 

    //接受一个服务端回话,再次一般初始化接受数据的对象 
    
    NSLog(@"返回数据类型:%@",[response textEncodingName]);  
    NSMutableData *d = [[NSMutableData alloc] init]; 
     self.data = d; 
    [d release]; 

 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 

    //接受返回数据,这个方法可能会被调用多次,因此将多次返回数据加起来 
     
    NSUInteger datalength = [data length]; 
    NSLog(@"返回数据量:%d",datalength); 
    [self.data appendData:data]; 

 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

    //连接结束 
     
    NSLog(@"%d:",[self.data length]); 
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); 
    NSString *mystr = [[NSString alloc] initWithData:_data encoding:enc]; 
   // string i 
    NSLog(@"最后的结果:%@",mystr); 
    self.label.text = mystr; 
    [mystr release]; 
    [self.connection release]; 

 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

    //链接错误 

 
@end 
简单说下:
1.最简单的网络链接,一个url,一个request,一个connection以及一个response返回。默认的是get请求。
2.data转码问题,这个一开始有点纠结。即,在最后我们要把NSData转化成NSString时候需要一个转码格式,一开始我习惯性的用了UTF-8,
然后发现转化后String 是Null,于是去打印了下请求返回的一些参数,显示的是GB2312~。
然后NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSString *mystr = [[NSString alloc] initWithData:_data encoding:enc];
就ok了。
4.代理方法的实现,基本以上几个都需要实现吧。还有更多可选方法可以根据自身需要去选择性实现。
作者:Nono_Love_Lilith
补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,