2

我正在尝试验证连接是否成功,但结果不稳定。当我尝试使用虚假网址进行同步请求时:

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

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

它挂了一段时间,最终返回:

 did_send = FALSE;

但是,如果我使用伪造的 url 执行异步请求:

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

我得到:

did_send = TRUE;

每次。我需要让异步请求正常工作,因为我能够设置超时并且不必在请求超时时挂起 60 秒,默认超时持续时间对于异步请求是不可更改的。有任何想法吗?

4

2 回答 2

7

尝试使用 nsurlconnection 类委托方法connection:didFailWithError: 这应该会给你一致的结果。

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

}
于 2010-08-16T18:55:08.873 回答
1

简单地实例化NSURLConnection什么都不做。它创建该类的一个实例,但不开始数据传输——即使请求无法满足,该对象通常也不为零。

相反,您的委托对象(self在您的代码中)需要实现NSURLConnectionDelegate(它将处理诸如“传入数据”或“错误条件”之类的回调。)然后您必须向NSURLConnection实例发送-start消息,这将在执行线程的运行循环中安排它.

有关NSURLConnectionDelegate协议的更多信息(包括有关在回调中做什么的信息),请参阅 Apple 的“ URL 加载系统编程指南”

于 2010-08-16T21:02:38.527 回答