Iphone 当服务器工作不正常时,如何显示警报?

Iphone 当服务器工作不正常时,如何显示警报?,iphone,objective-c,uialertview,Iphone,Objective C,Uialertview,我通过使用xml解析器解析url来获取列表。有时服务器工作不正常。然后,如何在服务器工作不正常时发出警报。我已经给出了下面的代码 -(NSMutableArray*)getCustomerList:(NSString *)count category:(NSString *)aCategory alphabetic:(NSString *)alphabeticValue favorite:(NSString *)isFavoriteString { [self updateStatus]

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

-(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以返回报告列表,当它返回零时,我将在视图控制器中显示警报

但当服务器工作不正常时,如何显示此警报

请帮我解决这个问题

谢谢,,
Madan mohan。

我相信有一种非常好的方法可以使用ParseXmlFileAtull来实现。我不喜欢那样

我所知道的是,如果您使用异步请求,那么所有这些都会为您处理。您创建了两个委托方法,RequestFinishedRequestFailed,当结果清晰时,将调用其中一个委托方法

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

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

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

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

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

为此,您可以使用nsurlRequest创建异步请求

NSURL*theURL=[[NSURL alloc]initWithString:@“**您的URL**”];
NSMutableURLRequest*theRequest=[[NSMutableURLRequest alloc]initWithURL:theURL];
[TheRequestSetTimeOutInterval:150];
[网址发布];
mURLConnection=[[NSURLConnection alloc]initWithRequest:theRequest委托:self];
[请求发布]

然后在委托方法中

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

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

-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据 {

}

//如果服务器端出现任何问题,此方法将调用 -(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误 {
[mResultData release]; mResultData=nil; [mURLConnection释放]; mURLConnection=nil;
//这里显示错误 UIAlertView*theAlert=[[UIAlertView alloc]initWithTitle:kAlertTitle消息:[错误本地化描述]委托:nil cancelButtonTitle:@“确定”其他ButtonTitles:nil]; [theAlert show]; [警报释放]; }

  • (无效)连接IDFinishLoading:(NSURLConnection*)连接 {

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


}

我知道这一点,我想,在xml解析器中是否有类似didFailWithError的方法来了解其行为。我在解析器中使用布尔值来处理这一问题。我不确定您希望我解释什么。如果您使用我指向的库并通过它发出http请求,您将可以使用这两个委托方法。如果您不使用该库——这很好——那么我相信还有另一种方法来处理超时或错误消息或其他任何东西。我只是碰巧不知道。我喜欢图书馆,因为它省去了很多麻烦。如果您想了解更多信息,请单击该链接。
[mResultData appendData:data];