views:

30

answers:

2

I am trying to validate weather the connection was successful but have been getting inconstant results. When I try to do an synchronous request using a bogus url with:

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (responseData)   
    {  
        did_send = TRUE;  
    }   
    else   
    {  
        did_send = FALSE;
    }

It hangs for a while an eventually returns:

 did_send = FALSE;

But if I do an asynchronous request using a bogus url with:

NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self ];
if (conn)   
    {  
        did_send = TRUE;  
    }   
    else   
    {  
        did_send = FALSE;
    }

I get:

did_send = TRUE;

every time. I need to get the asynchronous request working because I am able to set a timeout and not have to hang for 60 sec while the request times out with the default time out duration that is unchangeable with the asynchronous requests. Any ideas?

+2  A: 

Try using the nsurlconnection class delegate method connection:didFailWithError: This should give you consistent results.

-(void)getLocationsFromWebService {
    NSLog(@"in getLocationsFromWebService");


    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];


    NSURLRequest *theRequest = [NSURLRequest requestWithURL:self.locationsURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:100.0];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    self.requestType = @"GET";
    if (theConnection) {
        _responseData = [[NSMutableData data] retain ];
    } else {

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE]; 
    NSLog(@"in mapviewcontroller");
    NSLog(@"Error connecting - %@",[error localizedDescription]);
    [connection release];
    [_responseData release];
}

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

    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = [HTTPResponse statusCode];

    if (404 == statusCode || 500 == statusCode) {
        //[self.controller setTitle:@"Error Getting Parking Spot ....."];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];

        [connection cancel];
        NSLog(@"Server Error - %@", [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
    } else {
        if ([self.requestType isEqualToString:@"GET"])
            [_responseData setLength:0];
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if ([self.requestType isEqualToString:@"GET"])
        [_responseData appendData:data];
}

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

    if ([self.requestType isEqualToString:@"GET"]) {
        [self parseLocations:_responseData];
        [_responseData release];
    }
    [connection release];

}
ennuikiller
could you provide some examples as to how to use this method to solve my problem?
jcb344
I;ve added some of my own code to my answer. Please feel free to use it anyway you wish :)
ennuikiller
+1  A: 

Simply instantiating NSURLConnection does nothing. It creates an instance of that class but does not begin the data transfer--the object will generally be non-nil even if the request is impossible to satisfy.

Rather, your delegate object (self in your code) needs to implement NSURLConnectionDelegate (which will handle callbacks such as "incoming data" or "error condition.") Then you must send the NSURLConnection instance the -start message, which will schedule it on the executing thread's run loop.

For more information on the NSURLConnectionDelegate protocol (including information on what to do in your callbacks), see Apple's "URL Loading System Programming Guide."

Jonathan Grynspan