一.确认网络环境3G/WIFI
1. 添加源文件和framework
开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审查的。
Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。要在应用程序程序中使用Reachability,首先要完成如下两部:
1.1. 添加源文件:
在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。如下图:
1.2.添加framework:
将SystemConfiguration.framework 添加进工程。如下图:
2. 网络状态
Reachability.h中定义了三种网络状态:
typedef enum {
NotReachable = 0, //无连接
ReachableViaWiFi, //使用3G/GPRS网络
ReachableViaWWAN //使用WiFi网络
} NetworkStatus;
因此可以这样检查网络状态:
Reachability *r = [Reachability reachabilityWithHostName:@“ www.apple.com ”];
switch ([r currentReachabilityStatus]) {
case NotReachable:
// 没有网络连接
break;
case ReachableViaWWAN:
// 使用3G网络
break;
case ReachableViaWiFi:
// 使用WiFi网络
break;
}
3.检查当前网络环境
程序启动时,如果想检测可用的网络环境,可以像这样
// 是否wifi
+ (BOOL) IsEnableWIFI {
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
// 是否3G
+ (BOOL) IsEnable3G {
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
例子:
- (void)viewWillAppear:(BOOL)animated {
if (([Reachability reachabilityForInternetConnection].currentReachabilityStatus == NotReachable) &&
([Reachability reachabilityForLocalWiFi].currentReachabilityStatus == NotReachable)) {
self.navigationItem.hidesBackButton = YES;
[self.navigationItem setLeftBarButtonItem:nil animated:NO];
}
}
4. 链接状态的实时通知
网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户:
Reachability 1.5版本
// My.AppDelegate.h
#import "Reachability.h"
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
NetworkStatus remoteHostStatus;
}
@property NetworkStatus remoteHostStatus;
@end
// My.AppDelegate.m
#import "MyAppDelegate.h"
@implementation MyAppDelegate
@synthesize remoteHostStatus;
// 更新网络状态
- (void)updateStatus {
self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
}
// 通知网络状态
- (void)reachabilityChanged:(NSNotification *)note {
[self updateStatus];
if (self.remoteHostStatus == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil)
message:NSLocalizedString (@"NotReachable", nil)
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
// 程序启动器,启动网络监视
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// 设置网络检测的站点
[[Reachability sharedReachability] setHostName:@"www.apple.com"];
[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
// 设置网络状态变化时的通知函数
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
name:@"kNetworkReachabilityChangedNotification" object:nil];
[self updateStatus];
}
- (void)dealloc {
// 删除通知对象
[[NSNotificationCenter defaultCenter] removeObserver:self];
[window release];
[super dealloc];
}
Reachability 2.0版本
// MyAppDelegate.h
@class Reachability;
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
Reachability *hostReach;
}
@end
// MyAppDelegate.m
- (void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus status = [curReach currentReachabilityStatus];
if (status == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""
message:@"NotReachable"
delegate:nil
cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// ...
// 监测网络情况
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
hostReach startNotifer];
// ...
}
二.网络请求之同步与异步 GET与POST
1、同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作,
2、异步请求不会阻塞主线程,而会建立一个新的线程来操作,用户发出异步请求后,依然可以对UI进行操作,程序可以继续运行
3.GET格式为:http://www.best:50.cn: 8080/php/new_iphone.php?req=2&id=13/ GET将参数直接写在访问路径上
4.POST格式为:http://www.best:50.cn: 8080/php/new_iphone.php 在body里面添加参数 req=2&id=13;
5.GET请求和POST请求的区别为:GET请求将参数直接写在访问路径上。操作简单,不过容易被外界看到,安全性不高,地址最多255字节;
POST请求,将参数放到body里面。POST请求操作相对复杂,需要将参数和地址分开,不过安全性高,参数放在body里面,不易被捕获。
001 | 1、 同步GET请求 |
002 |
003 | //第一步,创建URL |
004 |
005 | NSURL *url = [NSURL URLWithString:@ "http://api.hudong.com/iphonexml.do?type=focus-c" ]; |
009 | //第二步,通过URL创建网络请求 |
0 |
011 | NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; |
012 |
013 | //NSURLRequest初始化方法第一个参数:请求访问路径,第二个参数:缓存协议,第三个参数:网络请求超时时间(秒) |
014 |
015 | 其中缓存协议是个枚举类型包含: |
016 |
017 | NSURLRequestUseProtocolCachePolicy(基础策略) |
018 |
019 | NSURLRequestReloadIgnoringLocalCacheData(忽略本地缓存) |
020 |
021 | NSURLRequestReturnCacheDataElseLoad(首先使用缓存,如果没有本地缓存,才从原地址下载) |
022 |
023 | NSURLRequestReturnCacheDataDontLoad(使用本地缓存,从不下载,如果本地没有缓存,则请求失败,此策略多用于离线操作) |
024 |
025 | NSURLRequestReloadIgnoringLocalAndRemoteCacheData(无视任何缓存策略,无论是本地的还是远程的,总是从原地址重新下载) |
026 |
027 | NSURLRequestReloadRevalidatingCacheData(如果本地缓存是有效的则不下载,其他任何情况都从原地址重新下载) |
028 |
029 | //第三步,连接服务器 |
030 |
031 | NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; |
032 |
033 | |
034 |
035 | NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding]; |
039 | NSLog(@ "%@" ,str); |
040 |
041 | 2、同步POST请求 |
042 |
043 | //第一步,创建URL |
044 |
045 | NSURL *url = [NSURL URLWithString:@ "http://api.hudong.com/iphonexml.do" ]; |
046 |
047 | //第二步,创建请求 |
048 |
049 | NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; |
050 |
051 | [request setHTTPMethod:@ "POST" ]; //设置请求方式为POST,默认为GET |
052 |
053 | NSString *str = @ "type=focus-c" ; //设置参数 |
054 |
055 | NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; |
056 |
057 | [request setHTTPBody:data]; |
058 |
059 | //第三步,连接服务器 |
061 |
063 | NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; |
064 |
067 | NSString *str1 = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding]; |
071 | NSLog(@ "%@" ,str1); |
072 | 3、异步GET请求 |
073 |
074 | //第一步,创建url |
075 |
076 | NSURL *url = [NSURL URLWithString:@ "http://api.hudong.com/iphonexml.do?type=focus-c" ]; |
077 |
078 | //第二步,创建请求 |
079 |
080 | NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; |
081 |
082 | //第三步,连接服务器 |
083 |
084 | NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; |
085 | 4、异步POST请求 |
086 |
087 | //第一步,创建url |
089 | NSURL *url = [NSURL URLWithString:@ "http://api.hudong.com/iphonexml.do" ]; |
091 | //第二步,创建请求 |
093 | NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; |
094 |
095 | [request setHTTPMethod:@ "POST" ]; |
097 | NSString *str = @ "type=focus-c" ; |
|
099 | NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; |
101 | [request setHTTPBody:data]; |
102 |
103 | //第三步,连接服务器 |
104 |
105 | NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; |
106 |
107 | 5、异步请求的代理方法 |
108 |
109 | //接收到服务器回应的时候调用此方法 |
110 |
111 | - ( void )connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response |
112 |
113 | { |
114 |
115 | NSHTTPURLResponse *res = (NSHTTPURLResponse *)response; |
116 |
117 | NSLog(@ "%@" ,[res allHeaderFields]); |
118 |
119 | self.receiveData = [NSMutableData data]; |
24 |
125 | } |
126 |
127 | //接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次 |
128 |
129 | -( void )connection:(NSURLConnection *)connection didReceiveData:(NSData *)data |
131 | { |
133 | [self.receiveData appendData:data]; |
|
135 | } |
136 |
137 | //数据传完之后调用此方法 |
138 |
139 | -( void )connectionDidFinishLoading:(NSURLConnection *)connection |
|
141 | { |
142 |
143 | NSString *receiveStr = [[NSString alloc]initWithData:self.receiveData encoding:NSUTF8StringEncoding]; |
144 |
145 | NSLog(@ "%@" ,receiveStr); |
|
147 | } |
148 |
149 | //网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法 |
150 |
151 | -( void )connection:(NSURLConnection *)connection |
152 |
153 | didFailWithError:(NSError *)error |
4 |
155 | { |
156 |
157 | NSLog(@ "%@" ,[error localizedDescription]); |
三.ASIHTTPRequest请求
ASIHTTPRequest是一款极其强劲的HTTP访问开源项目。让简单的API完成复杂的功能,
如:
异步请求,队列请求,GZIP压缩,缓存,断点续传,进度跟踪,上传文件,HTTP认证
在新的版本中,还加入了Objective-C闭包Block的支持,让我们的代码更加轻简灵活。
下面就举例说明它的API用法。
发起一个同步请求
同步意为着线程阻塞,在主线程中使用此方法会使应用Hang住而不响应任何用户事件。所以,在应用程序设计时,大多被用在专门的子线程增加用户体验,或用异步请求代替(下面会讲到)。
- (IBAction)grabURL:(id)sender
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if
(!error) {
NSString *response = [request responseString];
}
}
|
a, 用requestWithURL快捷方法获取ASIHTTPRequest的一个实例
b, startSynchronous 方法启动同步访问,
c, 由于是同步请求,没有基于事件的回调方法,所以从request的error属性获取错误信息。
d, responseString,为请求的返回NSString信息。
创建一个异步请求
异步请求的好处是不阻塞当前线程,但相对于同步请求略为复杂,至少要添加两个回调方法来获取异步事件。
下面异步请求代码完成上面同样的一件事情:
- (IBAction)grabURLInBackground:(id)sender
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (
void
)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (
void
)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
|
a,与上面不同的地方是指定了一个 "delegate",并用startAsynchronous来启动网络请求。
b,在这里实现了两个delegate的方法,当数据请求成功时会调用requestFinished,请求失败时(如网络问题或服务器内部错误)会调用requestFailed。
队列请求
提供了一个对异步请求更加精准丰富的控制。
如,可以设置在队列中,同步请求的连接数。往队列里添加的请求实例数大于maxConcurrentOperationCount时,请求实例将被置为等待,直到前面至少有一个请求完成并出列才被放到队列里执行。
也适用于当我们有多个请求需求按顺序执行的时候(可能是业务上的需要,也可能是软件上的调优),仅仅需要把maxConcurrentOperationCount设为“1”。
- (IBAction)grabURLInTheBackground:(id)sender
{
if
(![self queue]) {
[self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
}
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:
@selector
(requestDone:)];
[request setDidFailSelector:
@selector
(requestWentWrong:)];
[[self queue] addOperation:request];
//queue is an NSOperationQueue
}
- (
void
)requestDone:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
}
- (
void
)requestWentWrong:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
|
创建NSOperationQueue,这个Cocoa架构的执行任务(NSOperation)的任务队列。我们通过ASIHTTPRequest.h的源码可以看到,此类本身就是一个NSOperation的子类。也就是说它可以直接被放到"任务队列"中,并被执行。上面的代码队了队列的创建与添加操作外,其它代码与上一例一样。
请求队列上下文
a,可以设置一个上下文(userInfo)到request对象中,当请求响应完后可以通过访问request对象的userInfo获取里面的信息
b,为每一个请求实例设置不同的setDidFinishSelector / setDidFailSelector的回调方法
c,子类化ASIHTTPRequest,重写requestFinished: 与 failWithProblem:方法
ASINetworkQueues, 它的delegate提供更为丰富的功能
提供的更多的回调方法如下:
a,requestDidStartSelector,请求发起时会调此方法,你可以在此方法中跟据业务选择性的设置request对象的deleaget。
b,requestDidReceiveResponseHeadersSelector,当接受完响应的Header后设计此方法,这个对下载大数据的时候相当有用,你可以在方法里做更多业务上的处理。
c,requestDidFinishSelector,请求并响应成功完成时调用此方法
d,requestDidFailSelector,请求失败
e,queueDidFinishSelector,整个队列里的所有请求都结束时调用此方法。
它是NSOperationQueues的扩展,小而强大。但也与它的父类略有区别。如,仅添加到队列中其实并不能执行请求,只有调用[ queue g o]才会执行;一个正在运行中的队列,并不需要重复调用[ queue go ]。 默认情况下,队列中的一个请求如果失败,它会取消所有未完成的请求。可以设置[ queue setShouldCancelAllRequestsOnFailure:NO ]来修 正。 |
取消异步请求
首先,同步请求是不能取消的。
其次,不管是队列请求,还是简单的异步请求,全部调用[ request cancel ]来取消请求。
取消的请求默认都会按请求失败处理,并调用请求失败delegate。 如果不想调用delegate方法,则设置:[ request clearDelegatesAndCancel]; |
队列请求中需要注意的是,如果你取消了一个请求,队列会自动取消其它所有请求。
如果只想取消一个请求,可以设置队列:[ queue setShouldCancelAllRequestsOnFailure:NO ];
如果想明确取消所有请求:[ queue cancelAllOperations ];
安全的内存回收建议
request并没有retain你的delegate,所以在没有请求完的时候释放了此delegate,需要在dealloc方法里先取消所有请求,再释放请求实例,如:
- (
void
)dealloc
{
[request clearDelegatesAndCancel];
[request release];
...
[
super
dealloc];
}
|
向服务器端上传数据
ASIFormDataRequest ,模拟 Form表单提交,其提交格式与 Header会自动识别。
没有文件:application/x-www-form-urlencoded
有文件:multipart/form-data
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@
"Ben"
forKey:@
"first_name"
];
[request setPostValue:@
"Copsey"
forKey:@
"last_name"
];
[request setFile:@
"/Users/ben/Desktop/ben.jpg"
forKey:@
"photo"
];
[request addData:imageData withFileName:@
"george.jpg"
andContentType:@
"image/jpeg"
forKey:@
"photos"
];
|
如果要发送自定义数据:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[@
"This is my data"
dataUsingEncoding:NSUTF8StringEncoding]];
// Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:
[request setRequestMethod:@
"PUT"
];
|
下载文件
通过设置request的setDownloadDestinationPath,可以设置下载文件用的下载目标目录。
首先,下载过程文件会保存在temporaryFileDownloadPath目录下。如果下载完成会做以下事情:
1,如果数据是压缩的,进行解压,并把文件放在downloadDestinationPath目录中,临时文件被删除
2,如果下载失败,临时文件被直接移到downloadDestinationPath目录,并替换同名文件。
如果你想获取下载中的所有数据,可以实现delegate中的request:didReceiveData:方法。但如果你实现了这个方法,request在下载完后,request并不把文件放在downloadDestinationPath中,需要手工处理。
获取响应信息
信息:status , header, responseEncoding
[request responseStatusCode];
[[request responseHeaders] objectForKey:@
"X-Powered-By"
];
[request responseEncoding];
|
获取请求进度
有两个回调方法可以获取请求进度,
1,downloadProgressDelegate,可以获取下载进度
2,uploadProgressDelegate,可以获取上传进度
cookie的支持
如果Cookie存在的话,会把这些信息放在NSHTTPCookieStorage容器中共享,并供下次使用。
你可以用[ ASIHTTPRequest setSessionCookies:nil ] ; 清空所有Cookies。
当然,你也可以取消默认的Cookie策略,而使自定义的Cookie:
//Create a cookie
NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
[properties setValue:[@
"Test Value"
encodedCookieValue] forKey:NSHTTPCookieValue];
[properties setValue:@
"ASIHTTPRequestTestCookie"
forKey:NSHTTPCookieName];
[properties setValue:@
".allseeing-i.com"
forKey:NSHTTPCookieDomain];
[properties setValue:[NSDate dateWithTimeIntervalSinceNow:
60
*
60
] forKey:NSHTTPCookieExpires];
[properties setValue:@
"/asi-http-request/tests"
forKey:NSHTTPCookiePath];
NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
//This url will return the value of the 'ASIHTTPRequestTestCookie' cookie
request = [ASIHTTPRequest requestWithURL:url];
[request setUseCookiePersistence:NO];
[request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];
[request startSynchronous];
//Should be: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'
NSLog(@
"%@"
,[request responseString]);
|
大文件断点续传
0.94以后支持大文件的断点下载,只需要设置:
[ request setAllowResumeForFileDownloads:YES ];
[ request setDownloadDestinationPath:downloadPath ];
就可以了。
ASIHTTPRequest会自动保存访问过的URL信息,并备之后用。在以下几个场景非常有用:
1,当没有网络连接的时候。
2,已下载的数据再次请求时,仅当它与本地版本不样时才进行下载。
ASIDownloadCache 设置下载缓存
它对Get请求的响应数据进行缓存(被缓存的数据必需是成功的200请求):
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
|
当设置缓存策略后,所有的请求都被自动的缓存起来。
另外,如果仅仅希望某次请求使用缓存操作,也可以这样使用:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
|
多种的缓存并存
仅仅需要创建不同的ASIDownloadCache,并设置缓存所使用的路径,并设置到需要使用的request实例中:
ASIDownloadCache *cache = [[[ASIDownloadCache alloc] init] autorelease];
[cache setStoragePath:@
"/Users/ben/Documents/Cached-Downloads"
];
[self setMyCache:cache];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[self myCache]];
|
缓存策略
缓存策略是我们控制缓存行为的主要方式,如:什么时候进行缓存,缓存数据的利用方式。
以下是策略可选列表(可组合使用):
ASIUseDefaultCachePolicy | 这是一个默认的缓存策略“ASIAskServerIfModifiedWhenStaleCachePolicy”,这个很明白,见名知意(它不能与其它策略组合使用) |
---|---|
ASIDoNotReadFromCacheCachePolicy | 所读数据不使用缓存 |
ASIDoNotWriteToCacheCachePolicy | 不对缓存数据进行写操作 |
ASIAskServerIfModifiedWhenStaleCachePolicy | 默认缓存行为,request会先判断是否存在缓存数据。a, 如果没有再进行网络请求。 b,如果存在缓存数据,并且数据没有过期,则使用缓存。c,如果存在缓存数据,但已经过期,request会先进行网络请求,判断服务器版本与本地版本是否一样,如果一样,则使用缓存。如果服务器有新版本,会进行网络请求,并更新本地缓存 |
ASIAskServerIfModifiedCachePolicy | 与默认缓存大致一样,区别仅是每次请求都会 去服务器判断是否有更新 |
ASIOnlyLoadIfNotCachedCachePolicy | 如果有缓存在本地,不管其过期与否,总会拿来使用 |
ASIDontLoadCachePolicy | 仅当有缓存的时候才会被正确执行,如果没有缓存,request将被取消(没有错误信息) |
ASIFallbackToCacheIfLoadFailsCachePolicy | 这个选项经常被用来与其它选项组合使用。请求失败时,如果有缓存当网络则返回本地缓存信息(这个在处理异常时非常有用) |
如果设置了“defaultCachePolicy”则所有的请求都会使用此缓存。 |
缓存存储方式
你可以设置缓存的数据需要保存多长时间,ASIHTTPRequest提供了两种策略:
a,ASICacheForSessionDurationCacheStoragePolicy,默认策略,基于session的缓存数据存储。当下次运行或[ASIHTTPRequest clearSession]时,缓存将失效。
b,ASICachePermanentlyCacheStoragePolicy,把缓存数据永久保存在本地,
如:
ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url ];
[ request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy ];
|
另外,也可以使用clearCachedResponsesForStoragePolicy来清空指定策略下的缓存数据。
缓存其它特性
设置是否按服务器在Header里指定的是否可被缓存或过期策略进行缓存:
[[ ASIDownloadCache sharedCache ] setShouldRespectCacheControlHeaders:NO ];
|
设置request缓存的有效时间:
[ request setSecondsToCache:
60
*
60
*
24
*
30
];
// 缓存30天
|
可以判断数据是否从缓存读取:
[ request didUseCachedResponse ];
|
设置缓存所使用的路径:
[ request setDownloadDestinationPath:[[ ASIDownloadCache sharedCache ] pathToStoreCachedResponseDataForRequest:request ]];
|
实现自定义的缓存
只要简单的实现ASICacheDelegate接口就可以被用来使用。
使用代理请求
默认的情况下,ASIHTTPRequest会使用被设置的默认代理。但你也可以手动修改http代理:
// Configure a proxy server manually
ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url ];
[ request setProxyHost:@
"192.168.0.1"
];
[ request setProxyPort:
3128
];
// Alternatively, you can use a manually-specified Proxy Auto Config file (PAC)
// (It's probably best if you use a local file)
[request setPACurl:[NSURL URLWithString:@
"file:///Users/ben/Desktop/test.pac"
]];
|
ASIHTTPRequest, 请求的其它特性
iOS4中,当应用后台运行时仍然请求数据:
[ request setShouldContinueWhenAppEntersBackground:YES ];
|
是否有网络请求:
[ ASIHTTPRequest isNetworkInUse ]
|
是否显示网络请求信息在status bar上:
[ ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO ];
|
设置请求超时时,设置重试的次数:
[ request setNumberOfTimesToRetryOnTimeout:
2
];
|
KeepAlive的支持:
// Set the amount of time to hang on to a persistent connection before it should expire to 2 minutes
[ request setPersistentConnectionTimeoutSeconds:
120
];
// Disable persistent connections entirely
[ request setShouldAttemptPersistentConnection:NO ];
|
四.网络解析 XML与JSON区别
1.JSON VS XML
·可读性 JSON和XML的可读性可谓不相上下,一边是建议的语法,一边是规范的标签形式,很难分出胜负。
·可扩展性 XML天生有很好的扩展性,JSON当然也有,没有什么是XML能扩展,JSON不能的。
·编码难度 XML有丰富的编码工具,比如Dom4j、JDom等,JSON也有json.org提供的工具,但是JSON的编码明显比XML容易许多,即使不借助工具也能写出JSON的代码,可是要写好XML就不太容易了。
·解码难度 XML的解析得考虑子节点父节点,让人头昏眼花,而JSON的解析难度几乎为0。这一点XML输的真是没话说。
·流行度 XML已经被业界广泛的使用,而JSON才刚刚开始,但是在Ajax这个特定的领域,未来的发展一定是XML让位于JSON。到时Ajax应该变成Ajaj(Asynchronous Javascript and JSON)了。
2.解析 XML
通常有两种方式,DOM 和 SAX:
- DOM解析XML时,读入整个XML文档并构建一个驻留内存的树结构(节点树),通过遍历树结构可以检索任意XML节点,读取它的属性和值。而且通常情况下,可以借助XPath,直接查询XML节点。
- SAX解析XML,是基于事件通知的模式,一边读取XML文档一边处理,不必等整个文档加载完之后才采取操作,当在读取解析过程中遇到需要处理的对象,会发出通知对其进行处理。
一般在iOS平台下,比较常用的XML解析类库有如下几种:
- NSXMLParser,http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html ,这是一个SAX方式解析XML的类库,默认包含在iOS SDK中,使用也比较简单。
- libxml2,http://xmlsoft.org/,是一套默认包含在iOS SDK中的开源类库,它是基于C语言的API,所以使用起来可能不如NSXML方便。这套类库同时支持DOM和SAX解析,libxml2的SAX解析方式还是非常酷的,因为它可以边读取边解析,尤其是在从网上下载一个很大的XML文件,就可以一边下载一边对已经下载好的内容进行解析,极大的提高解析效率。
- TBXML,http://www.tbxml.co.uk/TBXML/TBXML_Free.html,这是一套轻量级的DOM方式的XML解析类库,有很好的性能和低内存占用,不过它不对XML格式进行校验,不支持XPath,并且只支持解析,不支持对XML进行修改。
- TouchXML,https://github.com/TouchCode/TouchXML,这也是一套DOM方式的XML解析类库,支持XPath,不支持XML的修改。
- KissXML,http://code.google.com/p/kissxml/,这是一套基于TouchXML的XML解析类库,和TouchXML相比,支持了XML的修改。
- TinyXML,http://www.grinninglizard.com/tinyxml/,这是一套小巧的基于C语言的DOM方式进行XML解析的类库,支持对XML的读取和修改,不直接支持XPath,需要借助另一个相关的类库TinyXPath才可以支持XPath。
- GDataXML,http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/XMLSupport/,这是一套Google开发的DOM方式XML解析类库,支持读取和修改XML文档,支持XPath方式查询。
- 那么对于如何在项目中选择合适的XML解析类库呢?网上已经有人对这几款XML类库做过分析和对比,可参考《How To Choose The Best XML Parser for Your iPhone Project》http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project 一文,基本比较准确和客观,文中建议:
- 如果是读取很小的XML文档,性能基本上没有什么差别,不过从调用的方便性来说,建议使用TouchXML、KissXML或GDataXML
- 如果是需要读取和修改XML文档,建议使用KissXML或GDataXML
- 如果需要读取非常大的XML文档,则建议使用libxml2或TBXML
- 如果你不想去调用第三方类库,那么使用NSXML也可以
安装说明
GDataXML
1. 从http://code.google.com/p/gdata-objectivec-client/downloads/list下载“gdata-objective-c client library.”
2. 解压缩文件,找到Source\XMLSupport,并且将其中的GDataXMLNode.h 和 GDataXMLNode.m文件拖到项目中
3. 选中项目,选中“Build Settings”标签页
4. 将Build Settings页中,顶部的“Basic”标签切换到“All”
5. 找到“Paths\Header Search Paths”项,并添加“/usr/include/libxml2”到列表中
6. 找到“Linking\Other Linker Flags”项,并添加“-lxml2”到列表中
首先在工程中新建一个xml文件,作为我们要解析的对象,新建方法是在工程中新建一个Empty的文件,命名为users.xml,然后添加内容:
- <?xml version="1.0" encoding="utf-8"?>
- <Users>
- <User id="001"]]>
- <name>Ryan</name>
- <age>24</age>
- </User>
- <User id="002"]]>
- <name>Tang</name>
- <age>23</age>
- </User>
- </Users>
接下来就可以开始解析了,在需要解析的文件中引入头文件:#import"GDataXMLNode.h"
我是新建的一个Empty工程,所以直接在AppDelegate.m中使用,代码如下:
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[[UIWindowalloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
- // Override point for customization after application launch.
- self.window.backgroundColor = [UIColorwhiteColor];
- [self.windowmakeKeyAndVisible];
- //获取工程目录的xml文件
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"users" ofType:@"xml"];
- NSData *xmlData = [[NSData alloc] initWithContentsOfFile:filePath];
- //使用NSData对象初始化
- GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0error:nil];
- //获取根节点(Users)
- GDataXMLElement *rootElement = [doc rootElement];
- //获取根节点下的节点(User)
- NSArray *users = [rootElement elementsForName:@"User"];
- for (GDataXMLElement *user in users) {
- //User节点的id属性
- NSString *userId = [[user attributeForName:@"id"] stringValue];
- NSLog(@"User id is:%@",userId);
- //获取name节点的值
- GDataXMLElement *nameElement = [[user elementsForName:@"name"] objectAtIndex:0];
- NSString *name = [nameElement stringValue];
- NSLog(@"User name is:%@",name);
- //获取age节点的值
- GDataXMLElement *ageElement = [[user elementsForName:@"age"] objectAtIndex:0];
- NSString *age = [ageElement stringValue];
- NSLog(@"User age is:%@",age);
- NSLog(@"-------------------");
- }
- returnYES;
- }
编译执行在控制台输出结果如下:
3.JSON解析
从IOS5开始,APPLE提供了对json的原生支持(NSJSONSerialization),但是为了兼容以前的ios版本,可以使用第三方库来解析Json。
本文将介绍TouchJson、 SBJson 、JSONKit 和 iOS5所支持的原生的json方法,解析国家气象局API,TouchJson和SBJson需要下载他们的库
TouchJson包下载: http://download.csdn.net/detail/enuola/4523169
SBJson 包下载: http://download.csdn.net/detail/enuola/4523177
JSONKit包下载:http://download.csdn.net/detail/enuola/4523160
下面的完整程序源码包下载:http://download.csdn.net/detail/enuola/4523223
PS:
国家气象局提供的天气预报接口
接口地址有三个:
http://www.weather.com.cn/data/sk/101010100.html
http://www.weather.com.cn/data/cityinfo/101010100.html
http://m.weather.com.cn/data/101010100.html
第三接口信息较为详细,提供的是6天的天气,关于API所返回的信息请见开源免费天气预报接口API以及全国所有地区代码!!(国家气象局提供),全国各城市对应这一个id号,根据改变id好我们就可以解析出来各个城市对应天气;
下面介绍四种方法解析JSON:
首先建立一个新的工程,(注意不要选择ARC机制)添加如下控件:
如上图所示。下面展出程序代码:
文件 ViewController.h 中:
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController
- @property (retain, nonatomic) IBOutlet UITextView *txtView;
- - (IBAction)btnPressTouchJson:(id)sender;
- - (IBAction)btnPressSBJson:(id)sender;
- - (IBAction)btnPressIOS5Json:(id)sender;
- - (IBAction)btnPressJsonKit:(id)sender;
- @end
(1)使用TouchJSon解析方法:(需导入包:#import "TouchJson/JSON/CJSONDeserializer.h")
- //使用TouchJson来解析北京的天气
- - (IBAction)btnPressTouchJson:(id)sender {
- //获取API接口
- NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"];
- //定义一个NSError对象,用于捕获错误信息
- NSError *error;
- NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
- NSLog(@"jsonString--->%@",jsonString);
- //将解析得到的内容存放字典中,编码格式为UTF8,防止取值的时候发生乱码
- NSDictionary *rootDic = [[CJSONDeserializer deserializer] deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:&error];
- //因为返回的Json文件有两层,去第二层内容放到字典中去
- NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];
- NSLog(@"weatherInfo--->%@",weatherInfo);
- //取值打印
- txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
- }
- //使用SBJson解析南阳的天气
- - (IBAction)btnPressSBJson:(id)sender {
- NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"];
- NSError *error = nil;
- NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
- SBJsonParser *parser = [[SBJsonParser alloc] init];
- NSDictionary *rootDic = [parser objectWithString:jsonString error:&error];
- NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];
- txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
- }
- - (IBAction)btnPressIOS5Json:(id)sender {
- NSError *error;
- //加载一个NSURL对象
- NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101180601.html"]];
- //将请求的url数据放到NSData对象中
- NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
- //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
- NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
- NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
- txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
- NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );
- }
- - (IBAction)btnPressJsonKit:(id)sender {
- //如果json是“单层”的,即value都是字符串、数字,可以使用objectFromJSONString
- NSString *json1 = @"{\"a\":123, \"b\":\"abc\"}";
- NSLog(@"json1:%@",json1);
- NSDictionary *data1 = [json1 objectFromJSONString];
- NSLog(@"json1.a:%@",[data1 objectForKey:@"a"]);
- NSLog(@"json1.b:%@",[data1 objectForKey:@"b"]);
- [json1 release];
- //如果json有嵌套,即value里有array、object,如果再使用objectFromJSONString,程序可能会报错(测试结果表明:使用由网络或得到的php/json_encode生成的json时会报错,但使用NSString定义的json字符串时,解析成功),最好使用objectFromJSONStringWithParseOptions:
- NSString *json2 = @"{\"a\":123, \"b\":\"abc\", \"c\":[456, \"hello\"], \"d\":{\"name\":\"张三\", \"age\":\"32\"}}";
- NSLog(@"json2:%@", json2);
- NSDictionary *data2 = [json2 objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
- NSLog(@"json2.c:%@", [data2 objectForKey:@"c"]);
- NSLog(@"json2.d:%@", [data2 objectForKey:@"d"]);
- [json2 release];
- }