如何在服务器无法正常工作时显示警报?

时间:2010-12-03 10:12:43

标签: iphone objective-c uialertview

我使用url通过使用xml解析器解析它来获取列表。有时服务器无法正常工作。然后如何在服务器无法正常工作时发出警报。我已经给出了以下代码

-(NSMutableArray*)getCustomerList:(NSString *)count category:(NSString *)aCategory alphabetic:(NSString *)alphabeticValue favorite:(NSString *)isFavoriteString
{
    [self updateStatus];
    if (internetConnectionStatus == NotReachable) 
    {

        UIAlertView *reachbleAlert = [[UIAlertView alloc] initWithTitle:@"message"
                                                                message: @"No network available alert"
                                                               delegate:self 
                                                      cancelButtonTitle:@"Ok"
                                                      otherButtonTitles: nil];
        [reachbleAlert show];   
        [reachbleAlert release];
        return 0;
    }
    else 
    {

        NSString *urlString=@"http:getCustomerList.jsp";
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [dataParser parseXMLFileAtURL:[NSURL URLWithString:urlString]];
        NSMutableArray *list =[dataParser getCustomerListFromParser];
        printf("\n url for Customer List%s",[urlString UTF8String]);
        printf("\n Customer List   %d",[list count]);
        return list;
    }
}

我发送参数给url,当它返回零时返回repctive列表我在视图控制器中显示警告。

但是当服务器无法正常工作时,如何显示此警报。

请帮我解决这个问题。

谢谢你, Madan mohan。

3 个答案:

答案 0 :(得分:0)

我确信使用parseXMLFileAtURL是一种非常好的方法。我不这样。

我所知道的是,如果您使用the excellent ASIHTTPRequest library发出异步请求,那么所有这些内容都会为您完成。您可以创建两个委托方法 RequestFinished RequestFailed ,并在结果清除时调用其中一个方法。

在RequestFinished中,你将解析响应对象的字符串部分。

在RequestFailed中,您将显示警报,然后决定如何从那里继续。

答案 1 :(得分:0)

在我看来: 首先对服务器执行请求操作以获得任何响应。

其次捕获BOOL变量中收到的响应。

最后当你的BOOL变量为TRUE时,执行所需的操作[例如解析..] 否则只显示带有正确错误消息的警报消息。

答案 2 :(得分:0)

为此你使用NSURLRequest创建异步请求

NSURL *theURL=[[NSURL alloc]initWithString:@"**YOUR URL**"]; NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:theURL]; [theRequest setTimeoutInterval:150]; [theURL release]; mURLConnection=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self]; [theRequest release]

然后在他们的委托方法

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 

{

if (mResultData == nil) //mResultData is NSData(member variable) 
    mResultData = [[NSMutableData alloc] init];
[mResultData setLength: 0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

[mResultData appendData:data];  

}

//此方法调用它们是否是来自服务器端的任何问题 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [mResultData发布];     mResultData = nil;     [mURLConnection发布];     mURLConnection = nil;
    //这里显示错误           UIAlertView * theAlert = [[UIAlertView alloc] initWithTitle:kAlertTitle message:[error localizedDescription] delegate:nil cancelButtonTitle:@“Ok”otherButtonTitles:nil];     [theAlert show];     [theAlert release]; }

  • (void)connectionDidFinishLoading:(NSURLConnection *)连接 {

    //这里你可以发送mResulData进行解析     //创建NSXMLParser

}

相关问题