iOS入门(三十八) XML解析 JSON解析

XML解析 JSON解析 
解析: 从事先规定好的格式中提取数据eXtensiable 

Markupp Language ( XML)
可扩展的标记性语言~
XXX
SAX解析 Simple API for XML 逐行解析数据
文件损坏的情况下,也有一半是可以读取的;
Dom解析 根据节点解析

Javascript Object Notation (JSON)
数组和字典组成
字典:{"key":"value"}
数组:["11","22","33"]

XMPP

XML解析 JSON解析
解析: 从事先规定好的格式中提取数据eXtensiable 

Markupp Language ( XML)
可扩展的标记性语言~
XXX
SAX解析 Simple API for XML 逐行解析数据
文件损坏的情况下,也有一半是可以读取的;
Dom解析 根据节点解析

Javascript Object Notation (JSON)
数组和字典组成
字典:{"key":"value"}
数组:["11","22","33"]

GeT 请求
PoSt 请求   ( + body)
连接服务器: 同步,异步
处理data: 图片音频视频    或者信息

块语法中引用的其他变量要使用 __block NSString * s =   

@interface RootViewController : UIViewController//异步请求的协议


@property ( nonatomic , retain ) UIImageView * imageview ;

@property ( nonatomic , retain ) UIButton * buttongetE;

@property ( nonatomic , retain ) UIButton * buttonget;

@property ( nonatomic , retain ) UIButton * buttonpostE;

@property ( nonatomic , retain ) UIButton * buttonpost ;


//负责接收异步请求的数据

@property ( nonatomic , retain ) NSMutableData * dataa;


@end



 

@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

- (void)dealloc

{

    [_imageview release];

    [_buttonget release];

    [_buttongetE release];

    [_buttonpost release];

    [_buttonpostE release];

    [_dataa release];

    [super dealloc];

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.imageview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 280, 280)];

    self.imageview.backgroundColor = [UIColor cyanColor];

    self.buttongetE = [[UIButton alloc] initWithFrame:CGRectMake(40, 350, 100, 30)];

    self.buttongetE.backgroundColor = [UIColor cyanColor];

    [self.buttongetE setTitle:@"同步GET" forState:UIControlStateNormal];

    self.buttonget = [[UIButton alloc] initWithFrame:CGRectMake(40, 400, 100, 30)];

    self.buttonget.backgroundColor = [UIColor cyanColor];

    [self.buttonget setTitle:@"异步GET" forState:UIControlStateNormal];

    self.buttonpostE = [[UIButton alloc] initWithFrame:CGRectMake(180, 350, 100, 30)];

    self.buttonpostE.backgroundColor = [UIColor cyanColor];

    [self.buttonpostE setTitle:@"同步POET" forState:UIControlStateNormal];

    self.buttonpost = [[UIButton alloc] initWithFrame:CGRectMake(180, 400, 100, 30)];

    self.buttonpost.backgroundColor = [UIColor cyanColor];

    [self.buttonpost setTitle:@"异步POST" forState:UIControlStateNormal];

    [self.view addSubview:_imageview];

    [self.view addSubview:_buttonpostE];

    [self.view addSubview:_buttonpost];

    [self.view addSubview:_buttongetE];

    [self.view addSubview:_buttonget];

    [_imageview release];

    [_buttonget release];

    [_buttongetE release];

    [_buttonpost release];

    [_buttonpost release];

    [self.buttonpost addTarget:self action:@selector(buttonClickedpost:) forControlEvents:UIControlEventTouchUpInside];

    [self.buttonpostE addTarget:self action:@selector(buttonClickedpostE:) forControlEvents:UIControlEventTouchUpInside];

    [self.buttonget addTarget:self action:@selector(buttonClickedget:) forControlEvents:UIControlEventTouchUpInside];

    [self.buttongetE addTarget:self action:@selector(buttonClickedgetE:) forControlEvents:UIControlEventTouchUpInside];


}

- ( void ) buttonClickedgetE:(UIButton * ) button

{

    NSLog(@"同步GET方法");

    NSString * string = @"http://cdn.gq.com.tw.s3-ap-northeast-1.amazonaws.com/userfiles/images_A1/6954/2011100658141857.jpg";

    NSURL * url = [NSURL URLWithString:string];

    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];

    [request setHTTPMethod:@"GET"];

    NSURLResponse * re = nil;

    NSError * error = nil;

    NSData * data = [NSURLConnection  sendSynchronousRequest:request returningResponse:&re error:&error];

    if (data != nil) {

        UIImage * image = [UIImage imageWithData:data];

        self.imageview.image = image;

    }else {

        NSLog(@"网络错误~");

    }

}

- ( void ) buttonClickedget:(UIButton * ) button

{

    NSLog(@"异步GET方法");

    //1、生成一个请求对象

    NSString * string = @"http://pic13.nipic.com/20110313/2686941_211532129160_2.jpg";

    NSURL * url = [NSURL URLWithString:string];

    NSMutableURLRequest * request  = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];

    [request setHTTPMethod:@"GET"];

    //2、 连接服务器,异步连接

    [NSURLConnection connectionWithRequest:request delegate:self];

    

    

}

- ( void ) buttonClickedpostE:(UIButton * ) button

{

    NSLog(@"同步POST方法");

    NSString * string = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";

    NSURL * url = [NSURL URLWithString:string];

    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];

    [request setHTTPMethod:@"POST"];

    //POST请求需要设置一个body

    NSString * bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";

    NSData * bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

    [request setHTTPBody:bodyData];

    //2、 连接服务器

    NSURLResponse * response = nil;

    NSError * error = nil;

    NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    //3、对data做一些事情(转换为照片或者而文字)

    if (data != nil) {

        NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"%@",str);

        [str release];

    }

}

- ( void ) buttonClickedpost:(UIButton * ) button

{

    NSLog(@"异步POST方法");

    NSString * string = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";

    NSURL * url = [NSURL URLWithString:string];

    NSMutableURLRequest * request  = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];

    [request setHTTPMethod:@"POST"];

    NSString * bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";

    NSData * bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

    [request setHTTPBody:bodyData];

    //连接服务器

//    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

//        //在这个语法块中,前面小括号中的参数是可以直接使用的参数,data是已经请求完毕的数据

//        NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

//        NSLog(@"%@",str);

//    }];

    

        [NSURLConnection connectionWithRequest:request delegate:self];

    

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    //代理实现的第一步: 获取到响应的httpheader

    NSLog(@"我是接收响应的分界线~");

    //每次接收到服务器响应的时候都创建一个新的Data对象

    self.dataa = [NSMutableData data];

    

}

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

{

    //代理实现的第二步:获取数据

    NSLog(@"我是接收数据的分界线");

    //每次接收到服务器传来的小块data 都把它们拼接到dataa 之后

    [self.dataa appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    //代理实现的第三步:数据接收完毕

    NSLog(@"我是接收完毕的小叮当~");

    //当执行这个方法 的时候,意味着dataa已经是一个完整的data 可以使用

//    UIImage * image = [UIImage imageWithData:self.dataa];

//    self.imageview.image = image;

    

//    NSString * str = [[NSString alloc] initWithData:self.dataa encoding:NSUTF8StringEncoding];

//    NSLog(@"%@",str);

    

    NSMutableDictionary * dic = [NSJSONSerialization JSONObjectWithData:self.dataa options:NSJSONReadingMutableContainers  error:nil];

    NSMutableArray * array = [dic objectForKey:@"news"];

    for (NSDictionary * dicc  in array) {

        NSLog(@"%@",[dicc objectForKey:@"id"]);

        NSLog(@"%@",[dicc objectForKey:@"title"]);

    }

}


你可能感兴趣的:(初级,ios)