网络,json,xml
通过NSURLConnection实现网络数据下载
通过NSURLConnection这个类来创建一个异步的请求
NSURLconnection提供了两种方式来实现链接,一种是同步的,另一种是异步的。异步的链接将会创建一个新的线程,这个线程将会来负责下载的动作。同步的链接将会堵塞当前线程,也就是说会造成当前的主线程堵塞,直到这个同步的线程运行完毕将会继续运行主线程。
同步和异步的主要区别就是运行的时候是否会创建一个新的现成,异步的会穿啊构建一个新的,而同步的并不会。
为了能够创建一个异步的请求连接,我们需要做如下操作:
1.创建一个NSString类型的URL链接字符串
2.把NSString类型转化成网络可识别的NSURL类型。
3.把我们的URL对象赋值到NSURLRequest对象中,如果是多个连接请求,请使用NSMutableURLRequest。
4.创建一个NSURLConnection的链接实例,然后把我们定义好的URL对象赋值过去。
我们可以创建一个异步的URL连接对象通过 sendAsynchronousRequest:queue:completionHandler 这个方法
这个方法参数如下:
sendAsynchronousRequest 一个NSURLRequest类型请求。
Queue 一个操作队列,wo我们可以很轻松的分配和初始化一个操作队列,然后可以根据我们需求添加到这个方法的参数中。
completionHandler 一个临时堵塞的监听不获取,当我们异步链接的操作完成之后,无论我们的异步操作是否成功,这个对象都能够接到如下三个参数:
1.一个NSURLResopne,这个对象是服务器返回给我们的数据包装对象。2.NSData,可选的,这个是我们通过URL请求返回的数据。3.NSError类型的对象,如果请求中有错误发生。
这个sendAsynchronousRequest:queue:completionHandler方法如果是添加在主线程上的将不会被调用,因此如果你想执行一个跟UI相关的任务,那么你的主线程就会被堵塞。
NSString *urlAsString = @"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest: urlRequest queue:queue completionHandler:^(
NSURLResponse *response,
NSData *data,
NSError *error
){
if([data length>0] && error == nil){
NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@",html);
}else if([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded.");
}else if(error != nil){
NSLog(@"Error happened = %@",error);
}
}];
如果你想保存从网络上下载的数据到你的硬盘中,那么你要在完成的block中将使用NSData的适当方法。
NSString *urlAsString = @"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest: urlRequest queue:queue completionHandler:^(
NSURLResponse *response,
NSData *data,
NSError *error
){
if([data length>0] && error == nil){
NSString * documentsDir= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString *filePath = [documentsDir stringByAppendingPathComponent:@"apple.html"];
[data writeToFile:filePath atomically:YES];
NSLog(@"Successfully saved the file to %@",filePath);
}else if([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded.");
}else if(error != nil){
NSLog(@"Error happened = %@",error);
}
}];
捕获异步链接中超时的问题
在创建一个URL请求对象并且准备传递给NSURLConnection,设置一下你的超时值。
NSString *urlAsString = @"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0f];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest: urlRequest queue:queue completionHandler:^(
NSURLResponse *response,
NSData *data,
NSError *error
){
if([data length>0] && error == nil){
NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@",html);
}else if([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded.");
}else if(error != nil){
NSLog(@"Error happened = %@",error);
}
}];
解析的时候超过了30秒,那么运行的时候这个访问对象将会给你一个超时的错误。
通过NSURLConnection创建一个同步的下载
通过NSURlConnection的sendSynchronousRequest:returningResponse:error:方法创建一个同步的网络连接。
在创建一个同步的网链接的时候我们需要明白一点,并不是我们的这个同步连接一定会堵塞我们的主线程,如果这个同步的链接是创建在主线程上的,那么这种情况下是会堵塞我们的主线程的。其他情况下是不一定会堵塞我们的主线程的。
这个例子中,我们将会尝试取回Yahoo主页的内容:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions{
NSLog(@"We are here ... ");
NSString *urlAsString =
@"http://www.yahoo.com";
NSURL *url = [NSURL
URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest
requestWithURL:url];
NSURLResponse *response =
nil;
NSError *error = nil;
NSLog(@"Firing synchronous url connection...");
NSData *data = [NSURLConnection
sendSynchronousRequest:urlRequest
returningResponse:&response error:&error];
if([data length]>0 && error ==
nil){
NSLog(@"%lu bytes of data was returned.",(unsigned
long)[data
length]);
}else
if ([data length] ==
0 && error == nil){
NSLog(@"No data was returned.");
}else
if (error != ni){
NSLog(@"Error happened = %@",error);
NSLog(@"We are done.");
}
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return
YES;
}
打印效果:
We are here...
Firing synchronous url connection... 194472 bytes of data was returned. We are done.
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions{
NSLog(@"We are here ... ");
NSString *urlAsString =
@"http://www.yahoo.com";
NSLog(@"Firing synchronous url connection...");
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_PRIORITY_DEFAULT,
0);
dispatch_async(dispatchQueue, ^(void){
NSURL *url = [NSURL
URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest
requestWithURL:url];
NSURLResponse *response =
nil;
NSError *error = nil;
NSData *data = [NSURLConnection
sendSynchronousRequest:urlRequest
returningResponse:&response error:&error];
if([data length]>0 && error ==
nil){
NSLog(@"%lu bytes of data was returned.",(unsigned
long)[data
length]);
}else
if ([data length] ==
0 && error == nil){
NSLog(@"No data was returned.");
}else
if (error != ni){
NSLog(@"Error happened = %@",error);
}
});
NSLog(@"We are done.");
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return
YES;
}
打印效果:
We are here...
Firing synchronous url connection... We are done.
194326 bytes of data was returned.
看了如上的代码是不是感觉特别奇怪,怎么
we are done 还是放在最后面的,怎么现在打印到前面的了,这
就是我们要证明的,其实同步的程序并不一定会堵塞我们的主线程的。只要我们的这个同步的线程并不是建立
在我们的主线程上的,如果我们把他添加到
GCD 队列池中的话,那么我们的主线程也就没有被堵塞。这样同
步的概念实际上也可以看做是某种程度上的异步。
通过NSMutableURLRequest包装URL的请求形式
你想在传给一个请求链接前能适应一个URL的不同HTTP头及其设置。
通过NSMutableURLRequest代替NSURLRequest
通常我们的URL请求可能是一组,并不是一个,而且我们的URL请求也是会不停的根据流程在变化的,所以我们要仍然使用NSURLRequest这个对象是不能随时的进行一个URL的变化的,因此我们的NSmutableURLRequest这个对象,可以用来解决我们这个问题。
在分配和初始化了请求之后修改请求url的timeout:
NSString *urlAsString = @"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
在分配和初始化了请求之后设置请求的URL和timeout:
NSString *urlAsString = @"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *request = [NSMutableURLRequest new];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setURL:url];
通过NSURLConnection发送一个HTTP GET 请求
你想通过Http协议向服务器发送一个Get的包装请求,并在这个请求中添加了一些请求参数
NSString *urlAsString = @"http://pixolity.com/get.php";
urlAsString = [urlAsString stringByAppendingString:@"?param1 = First"];
urlAsString = [urlAsString stringByAppendingString:@"¶m2 = Second"];
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandle:^(NSURLResponse *response,NSData *data,NSError *error){
if([data length]>0 && error == nil){
NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8String];
NSLog(@"HTML = %@",html);
}else if([data length] ==0 && error == nil ){
NSLog(@"Nothing was downloaded.");
}else if(error != nil){
NSLog(@"Error happened = %@",error);
}
}];
编译效果:
html =
<html>
<head>
<title>IOS 5 Programming Cookbook GET Example</title>
</head>
<body>
<b>HTTP Method</b>=GET<br>Query String</b> =Array([param1] =>First [param2]=>Second)
</body>
</html>
通过NSURLConnection发送一个POST表单请求
NSString *urlAsString = @"http://pixolity.com/get.php";
urlAsString = [urlAsString stringByAppendingString:@"?param1 = First"];
urlAsString = [urlAsString stringByAppendingString:@"¶m2 = Second"];
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"POST"];
NSString *body = @"bodyParam1 = BodyValue1&bodyParam2 = BodyValue2";
[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandle:^(NSURLResponse *response,NSData *data,NSError *error){
if([data length]>0 && error == nil){
NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8String];
NSLog(@"HTML = %@",html);
}else if([data length] ==0 && error == nil ){
NSLog(@"Nothing was downloaded.");
}else if(error != nil){
NSLog(@"Error happened = %@",error);
}
}];
编译效果:
html =
<html>
<head>
<title>IOS 5 Programming Cookbook POST Example</title>
</head>
<body>
<b>HTTP Method</b>=POST<br>Query String</b> =Array([param1] =>First [param2]=>Second)
<b>Body parameters</b> = Array([bodyParam1]=>BodyValue1 [bodyParam2]=>BodyValue2)
</body>
</html>
通过NSURLConnection发生HTTP DELETE 请求
通过HTTP协议向服务器发送一个删除资源的一个URL链接,同时你也可以给这个请求添加参数
将[urlRequest setHTTPMethod:@"POST"];
改为[urlRequest setHTTPMethod:@"DELETE"];
通过NSURLConnection发送HTTP PUT请求
就像发送POST,GET,DELETE请求一样。将相应位置设置成PUT;
把Array和Dictionaries序列化成JSON对象
通过NSJSONSerialization这个类的dataWithJSONObject:options:error:方法来实现
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
[dictionary setValue:@"Anthony" forKey:@"First Name"];
[dictionary setValue:@"Robbins" forKey:@"Last Name"];
[dictionary setValue:[NSNumber numberWithUnsignedInteger:51] forKey:@"Age"];
NSArray *arrayOfAnthonysChildren = [[NSArray alloc]initWithObjects:@"Anthony's Son 1",@"Anthony's Danghter 1",@"Anthony's Son 2",@"Anthony's Son 3",@"Anthony's Daughter 2",nil];
[dictionary setValue: arrayOfAnthonysChildren forKey:@"children"];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithISONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
if([jsonData length]>0 && error == nil){
NSLog(@"Successfully serializes the dictionary into data = %@,jsonData");
}else if([jsonData length] == 0 && error == nil){
NSLog(@"No data returned after serialization.");
}else if(error != nil){
NSLog(@"An error happened = %@",error);
}
运行结果:
successfully serialized the dictionary into data.
JSON String = {
"Last Name" : "Robbins", "First Name" : "Anthony", "children" : [
"Anthony's Son 1", "Anthony's Daughter 1", "Anthony's Son 2", "Anthony's Son 3", "Anthony's Daughter 2"
],
"Age" : 51 }
把json数据转化成Arrays 或者Dictionaries
希望把一个json数据解析出来放在数据或者字典里保存,通过NSJSONSerialization 这个类的 JSONObjectWithData:options:error:方法来实现
error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
if(jsonObject != nil && error == nil){
NSLog(@"Successfully deserialized … ");
if([jsonObject isKindOfClass:[NSDictionary class]]){
NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(@"Dersialized JSON Dictionary = %@",deserializedDictionary);
}else if([jsonObject isKindOfClass:[NSArray class]]){
NSArray *deserializedArray = (NSArray *)jsonObject;
NSLog(@"Dersialized JSON Array =%@",deserializedArray);
}
}
else if(error != nil){
NSLog(@"An error happened while deserializing the JSON data.");
}
分享到:
相关推荐
iOS下解析json数据,自己亲自使用过,挺好用的。 使用JSONKit的解析方法:(需导入包:#import "JSONKit/JSONKit.h") - (void)printJson{ //如果json是“单层”的,即value都是字符串、数字,可以使用...
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。四种Json解析的方法实现如下:
移动开发抓包工具和IOS JSON解析工具,希望对大家有用
iOS xml解析和json解析demo
一个第三方类库,主要负责IOS中的xml解析和JSON解析
本demo 是解析json数据 利用 苹果自带的解析技术解析json 也是json 解析数据 推荐的和优先考虑的
iOS5新增了JSON解析的API,我们将其和其他五个开源的JSON解析库进行了解析速度的测试,下面是测试的结果
JSONKIT 为IOS解析最快的JSON解析工具类.
完美支持UNITY中JSON的解析和生成。IOS和Android 完全支持,比SimpleJSON更强大。c#版本
简单的使用了json解析、AFNetworking和MJExtension
1.简单的ios-json解析 2.在程序里的json串,解析为3个数组 3.涉及字典
ios获取网络json数据本地解析显示
写了一个使用json官方工具包解析json的例子,共参考,可以去博客看
iOS json生成model工具,mac电脑上dmg格式,安装即可使用,自动生成model
iOS,XML解析, JSON解析, @"SAX解析",@"Dom解析第一种方式",@"Dom解析第二种方式",@"系统解析",@"字符串解析",@"数组解析",@"字典解析",@"Data解析
ios下json数据解析的TouchJson的库, 欢迎下载
1.JSON 包括系统方法的JSON ,以及三方JSONKit 2.XML 系统的解析(SAX方式),三方google的一个解析方式(DOM解析) SAX 解析: 这是苹果系统自带的xml解析方法,特点就是:逐行解析, 这样方式效率低,但是可以快速...
ios学习笔记中用到的美化按钮的背景图,iphone风格
在学习C#,JSON解析时,突然想到IOS的JSON解析KVC的方式, 网上查阅了些资料,做了个解析JSON多层数据展示的Demo
ios学习笔记---simpleping 适合初学者练习使用。只是一个小的demo