代码之家  ›  专栏  ›  技术社区  ›  jcb344

NSURLConnection验证

  •  2
  • jcb344  · 技术社区  · 16 年前

    我正在尝试验证连接是否成功,但得到了不稳定的结果。当我尝试使用假URL执行同步请求时:

    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秒,而默认超时持续时间对于异步请求是不可更改的。有什么想法吗?

    2 回复  |  直到 16 年前
        1
  •  7
  •   ennuikiller    16 年前

    尝试使用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];
    
    }
    
        2
  •  1
  •   Jonathan Grynspan    16 年前

    简单的实例化 NSURLConnection 什么也不做。它创建了该类的一个实例,但没有开始数据传输——对象通常是非零的,即使请求不可能满足。

    相反,你的委托对象( self 在您的代码中)需要实现 NSURLConnectionDelegate (它将处理回调,如“传入数据”或“错误条件”),然后必须发送 非连接 实例 -start 消息,它将在执行线程的运行循环上调度它。

    有关 NSURLConnectionDelegate(NSURLConnectionDelegate) 协议(包括有关回调操作的信息),请参见Apple的 URL Loading System Programming Guide ."

    推荐文章