网络请求数据解析

重要!!!!

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueuecompletionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

    

    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&connectionError];

    NSLog(@"%@", dic);

    

}];

同步

// **********************************************************

// GET请求

// 创建一个URL

NSURL *url = [NSURL URLWithString:strURLEncode];

// 创建一个请求

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSURLResponse *response = nil;

NSError *error = nil;

// 第一个参数:创建的请求

// 第二个参数:返回的答复

// 第三个参数:错误信息

// 发送请求

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

// 第一种

NSMutableDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

// 第二种

// 需要引JSONKit.h(第三方)

NSMutableDictionary *dataDic = [data objectFromJSONData];


// **********************************************************

// POST请求

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

NSURL *url = [NSURL URLWithString:strURL];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];


// 设置请求的方式,默认是GET请求

[request setHTTPMethod:@"POST"];

NSString *bodyStr = @"date=20131129&startRecord=5&len=5&udid=1234567890&terminalType=Iphone&cid=215";

// 把字符串转换成NSData类型

NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

// 把bodyData添加到request中

[request setHTTPBody:bodyData];

NSURLResponse *response = nil;

NSError *error = nil;

// 发送请求

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

// 解析数据

NSMutableDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

异步

NSLog(@"GET异步请求");

NSString *strURL = @"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg";

NSURL *url = [NSURL URLWithString:strURL];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];


[NSURLConnection connectionWithRequest:request delegate:self];


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

// 初始化,接受数据的容器

self.data = [NSMutableData data];

}


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

// appendData是累加的意思

[self.data appendData:data];

}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

// 数据请求结束

self.myImageView.image = [UIImage imageWithData:self.data];

}


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

NSLog(@"%@", error);

}


封装 

Block block = ^(id result) {

    NSMutableDictionary *dic = result;

    self.arr = dic[@"events"];

    [self.tblView reloadData];

};

[NetWorkingTool netWorkingToolStr:strURL block:block];

/**********************/

+ (void)netWorkingToolStr:(NSString *)strURL block:(Block)myBlock {

    NSString *strURLEncode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:strURLEncode];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueuecompletionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&connectionError];

        myBlock(result);

    }];

}


你可能感兴趣的:(网络请求数据解析)