开发者

How to show an alert when the server is not working properly?

I am getting list using url by doing parsing it using xml parser. sometimes the server is not working properly. then how to give an alert whe开发者_如何学Gon server is not working properly. I have given the code below

-(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;
    }
}

I am sending parameter to url to return repctive list when it returns zero I am dispalying alert in view controller.

but when server is not working properly then how to display this alert.

Please help me out of this.

Thank you, Madan mohan.


In my opinion : First perform the request operation to the server for any response.

Secondly Catch the the response received in a BOOL variable .

Finally when your BOOL variable is TRUE perform the desired operation [Such as parsing..] Otherwise just show an alert message with a proper error message.


I'm sure there's a perfectly nice way to do it with parseXMLFileAtURL. I don't that way.

What I do know is if you use the excellent ASIHTTPRequest library to make an asynchronous request, all of that stuff is taken care of for you. You create two delegate methods, RequestFinished and RequestFailed, and one of them will be called when the result is clear.

In RequestFinished you'd parse the string portion of the response object.

In RequestFailed you'd display the alert and then decide how to proceed from there.


for this u create the asynchronous request usingNSURLRequest

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]

and then in their delegate method

- (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];  

}

// this method calls if their is any problem from server side -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[mResultData release]; mResultData = nil; [mURLConnection release]; mURLConnection = nil;
//here the show the error UIAlertView *theAlert=[[UIAlertView alloc]initWithTitle:kAlertTitle message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [theAlert show]; [theAlert release]; }

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    //here u could send mResulData for parsing // create NSXMLParser

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜