2013-03-21 124 views
0

我在使用NSURLConnection時遇到連接丟失問題。我正在使用NSURLConnection進行異步下載。我正在下載大小約80MB的大文件。每次正確處理文件時,我都會將收到的數據寫入文件。在某段時間後,我在名爲didFailWithError的NSURLConnection委託的方法中出現連接「連接丟失」的錯誤。如果我在Mac上的模擬器中執行,那麼它將需要很長時間,但是文件可以成功下載,而不會出現「連接丟失」錯誤。任何建議如何避免這個錯誤?或者這個錯誤背後的原因是什麼?連接使用NSURLConnection時丟失錯誤

讓我知道是否需要任何細節。請注意,我閱讀過類似的文章,但它並沒有幫助我。

+0

就可以顯示相關的代碼? – Raptor 2013-03-21 06:08:51

+0

這裏是一個建議:http://stackoverflow.com/questions/6926518/how-to-download-large-files-from-web-service-iphone – rptwsthi 2013-03-21 06:43:37

回答

-1

找到下面的代碼片段,讓我知道,如果需要了解更多信息:

-(void) startDownloadFromURL:(NSString*)URLString 
{ 
    if(URLString == nil) 
    { 
     [delegate DownloadFailed:-1]; 
     return; 
    } 
    //self.pstrBaseFilePath = filePath; 
    URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 

    NSMutableURLRequest* pRequest = [[NSMutableURLRequest alloc] init]; 
    [pRequest setURL:[NSURL URLWithString:URLString]]; 

    if(gpUserDataManager.pstrSessionID == nil) 
     return; 

    [pRequest addValue:[@"ASessionID=" stringByAppendingString:gpUserDataManager.pstrSessionID] forHTTPHeaderField:@"Cookie"]; 

    [pRequest setHTTPMethod:@"GET"]; 
    [pRequest setTimeoutInterval:180]; 


    self.urlConnection = [[NSURLConnection alloc] initWithRequest:pRequest delegate:self]; 

    [urlConnection start]; 
} 

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

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 

    if([httpResponse statusCode] == 200) 
    { 

    } 
    else 
    { 
     //Start.NOODLE-13304 
     /* NSInteger iResponseCode = [httpResponse statusCode]; 
     NSString* pstrStr = [NSString stringWithFormat:@"%d", iResponseCode]; 

     //pTheConnection = nil; 

     [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Response Error", @"") 
     message:pstrStr 
     delegate:nil 
     cancelButtonTitle:NSLocalizedString(@"OK", @"") 
     otherButtonTitles:nil] show]; 
     */ 
     [AUserDataManager ProcessResponseCode:httpResponse.statusCode]; 
     [self.urlConnection cancel]; 
     [delegate DownloadFailed:httpResponse.statusCode]; 
     //End.NOODLE-13304 
    } 

    //[self.pRecvdata setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    if(self.recvdData == nil) 
    { 
     self.recvdData = [[NSMutableData alloc] init]; 
    } 

    [self.recvdData appendData:data]; 
} 


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 

    // bIsResponseOK = FALSE; 
    // 
    // [NSThread detachNewThreadSelector: @selector(SpinEnd) toTarget:self withObject:nil]; 
    // 
    // pTheConnection = nil; 

    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Connection Error", @"") 
           message:[error localizedDescription] 
           delegate:nil 
         cancelButtonTitle:NSLocalizedString(@"OK", @"") 
         otherButtonTitles:nil] show]; 
    [self.urlConnection cancel]; 
    [delegate DownloadFailed:-1]; 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [connection cancel]; 
    [delegate DownloadCompleted:self.recvdData]; 
} 

DownloadRequest *request = [[DownloadRequest alloc] init]; 
request.delegate = self; 
[request startDownloadFromURL:strURL];