服务器反馈为空,反馈服务器端error的处理

1.首先明确下顺序:

Eg:"POST"情况:string->NSData,NSMutableURLrequest,NSURLConnection,NSURLResponse

staticNSString*body

=@"aaaaaa";//要POST的输入string

staticNSString*URLString

=@"http://earthquake.usgs.gov/eqcenter/catalogs/7day-M2.5.xml";

NSData *dataBody

= [NSData dataWithBytes:

[stringBody UTF8String] length:

[body length]];//将string封装成NSData类型的数据

NSMutableURLRequest*request

= [[NSMutableURLRequestalloc]initWithURL:[NSURLURLWithString: URLString]];

//发送请求,正式发送

[request setHTTPMethod:@"POST"];

[request setHTTPBody:dataBody];

NSURLResponse*response;

NSError *error;

[NSURLConnectionsendSynchronousRequest:requestreturningResponse:&responseerror:&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 = [[UIAlertViewalloc]initWithTitle:NSLocalizedString(@"Error

Title",@"")message:errorMessagedelegate:nilcancelButtonTitle:@"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

= [NSErrorerrorWithDomain:NSCocoaErrorDomaincode:[errorcode]userInfo:[NSDictionarydictionary]];

//本地化描述具体的错误,用NSInteger返回ui,告知用户

NSString *errorMessage

= [locatedError localizedDescription];

//以下是弹出警告,为了UI的友好性要求

UIAlertView*alertView

= [[UIAlertViewalloc]initWithTitle:NSLocalizedString(@"Error

Title",@"")message:errorMessagedelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];

[alertView show];

[alertView release];

}

OVER,

以上是从xml获取信息移植到自定义app中的错误处理过程,~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值