网络,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.");
}