网络错误的基本处理
2011-05-19 10:05
183 查看
1.首先明确下顺序:
Eg:"POST"情况:string->NSData,NSMutableURLrequest,NSURLConnection, NSURLResponse
static NSString *body = @"aaaaaa";//要POST的输入string
static NSString *URLString = @"http://earthquake.usgs.gov/eqcenter/catalogs/7day-M2.5.xml";
NSData *dataBody = [NSData dataWithBytes: [stringBody UTF8String] length: [body length]];//将string封装成NSData类型的数据
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: URLString]];
//发送请求,正式发送
[request setHTTPMethod:@"POST"];
[request setHTTPBody:dataBody];
NSURLResponse *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];//发送同步连接+反馈请求
//在NSCocoaErrorsDomain领域中,除非你知道具体的CFNetWorkError类型,非则用[Error code]便利构造
if(error){
NSError *locatedError = [NSError errorWithDomain:NSCocoaErrorDomain code:[error code]userInfo:[NSDictionarydictionary]];
//本地化描述具体的错误,用NSInteger返回ui,告知用户
NSString *errorMessage = [locatedError localizedDescription];
//以下是弹出警告,为了UI的友好性要求
UIAlertView *alertV iew = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error Title", @"")message:errorMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
2.Eg:默认"GET"情况:NSMutableURLrequest,NSURLConnection, NSURLRespons,String->NSData
>>>同上
[URLRequest setHTTPMethod:@"GET"];//默认为GET,否则是POST,也可不加
//连接失败,则抛出异常,如有2中delegate监听,亦可不加
NSAssert(self.earthquakeFeedConnection != nil, @"Failure to create URL connection.");
//连接等待标志,必加的
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
2.哦了,以上的连接是最经典的代码,可以进行NSURLConnection delegate监听了~
1>
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//这里写些对response的处理,一般是些error处理,可以不在这里处理直接跳转到3>
}
2>
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
//这里写些对data数据的处理,一般是初始化数据后,将data写入
}
3>
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//在NSCocoaErrorsDomain领域中,除非你知道具体的CFNetWorkError类型,非则用[Error code]便利构造
NSError *locatedError = [NSError errorWithDomain:NSCocoaErrorDomain code:[error code] userInfo:[NSDictionary dictionary]];
//本地化描述具体的错误,用NSInteger返回ui,告知用户
NSString *errorMessage = [locatedError localizedDescription];
//以下是弹出警告,为了UI的友好性要求
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error Title", @"") message:errorMessagedelegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
OVER, 以上是从xml获取信息移植到自定义app中的错误处理过程,~~
Eg:"POST"情况:string->NSData,NSMutableURLrequest,NSURLConnection, NSURLResponse
static NSString *body = @"aaaaaa";//要POST的输入string
static NSString *URLString = @"http://earthquake.usgs.gov/eqcenter/catalogs/7day-M2.5.xml";
NSData *dataBody = [NSData dataWithBytes: [stringBody UTF8String] length: [body length]];//将string封装成NSData类型的数据
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: URLString]];
//发送请求,正式发送
[request setHTTPMethod:@"POST"];
[request setHTTPBody:dataBody];
NSURLResponse *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];//发送同步连接+反馈请求
//在NSCocoaErrorsDomain领域中,除非你知道具体的CFNetWorkError类型,非则用[Error code]便利构造
if(error){
NSError *locatedError = [NSError errorWithDomain:NSCocoaErrorDomain code:[error code]userInfo:[NSDictionarydictionary]];
//本地化描述具体的错误,用NSInteger返回ui,告知用户
NSString *errorMessage = [locatedError localizedDescription];
//以下是弹出警告,为了UI的友好性要求
UIAlertView *alertV iew = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error Title", @"")message:errorMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
2.Eg:默认"GET"情况:NSMutableURLrequest,NSURLConnection, NSURLRespons,String->NSData
>>>同上
[URLRequest setHTTPMethod:@"GET"];//默认为GET,否则是POST,也可不加
//连接失败,则抛出异常,如有2中delegate监听,亦可不加
NSAssert(self.earthquakeFeedConnection != nil, @"Failure to create URL connection.");
//连接等待标志,必加的
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
2.哦了,以上的连接是最经典的代码,可以进行NSURLConnection delegate监听了~
1>
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//这里写些对response的处理,一般是些error处理,可以不在这里处理直接跳转到3>
}
2>
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
//这里写些对data数据的处理,一般是初始化数据后,将data写入
}
3>
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//在NSCocoaErrorsDomain领域中,除非你知道具体的CFNetWorkError类型,非则用[Error code]便利构造
NSError *locatedError = [NSError errorWithDomain:NSCocoaErrorDomain code:[error code] userInfo:[NSDictionary dictionary]];
//本地化描述具体的错误,用NSInteger返回ui,告知用户
NSString *errorMessage = [locatedError localizedDescription];
//以下是弹出警告,为了UI的友好性要求
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error Title", @"") message:errorMessagedelegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
OVER, 以上是从xml获取信息移植到自定义app中的错误处理过程,~~
相关文章推荐
- 网络通信中的错误处理(记录)
- Retrofit请求数据对错误以及网络异常的处理
- iOS开发之网络错误分层处理
- iOS网络高级编程:iPhone和iPad的企业应用开发之错误处理
- 网络管理员工作错误处理常见十宗罪
- 错误处理:此网页不允许使用基本类型"System.Web.UI.MasterPage”该类型未注册为安全类型
- 练习1-24: 编写一个程序,查找C语言程序中的基本语法错误,如圆括号,方括号以及花括号不配对等。要正确的处理引号(包括单引号,双引号)~转移字符序列与注释(如果读者想把该程序编写成完全通用的程序,难度会比较大。)
- 非禁用validateRequest=false使用Page_Error()错误处理[摘自网络]
- Hive安装及基本命令整理超详细超详细超详细重要的事情说三遍以及连接错误问题处理
- 【读书笔记】iOS网络-错误处理的经验法则
- 《WF编程》系列之30 - 基本活动:错误处理
- 【读书笔记】iOS网络-错误处理的经验法则
- linux编程之网络编程错误处理
- tableViewCell cell 图片网络下载的 基本处理原理
- 10:ASP.NET复习系列- ASP.NET基本知识点- ASP.NET中的错误处理和程序优化
- .net WCF 套接字连接中断,可能是由于消息处理错误,或者远程宿主接受超时引起,或者是底层网络资源问题导致,本地套接字时间
- IOS网络编程之错误处理
- protell 99网络表装载错误处理
- iOS网络请求中的错误处理
- Retrofit请求数据对错误以及网络异常的处理