2010-07-01 80 views
1

我又來了!我使用NSURL來獲取文件然後解析它。我一直在尋找幾個小時,現在我正試圖在我的應用程序中實現一個進度條。我知道我首先需要獲取文件大小,然後繼續更新下載的數據量,因爲我繼續下載。我已經看到了使用「ASIHTTPRequest」的例子,但是有什麼方法可以用我已有的方法來完成它?在解析之前獲取URL文件大小

這裏是我開始下載的地方。

-(void)parseNewData { 


    //start network activity spinner and release controller when done 
    parserDone = NO; 
    [root downloadIcon]; 


    //create pool to avoid memory leak 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 


    // get the XML path and start parsing 
    NSURL *pathURL = [NSURL URLWithString:@"http://www.mysite.com/myfile.xml"]; 




    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:pathURL]; 
    [parser setDelegate:self]; 
    [parser parse]; 

    //drain pool 
    [pool drain]; 
    [pool release]; 

} 

有人點我在正確的方向上如何獲得文件大小,那麼如何更新多少我已經下載。提前致謝!

回答

1

如果您想要獲取文件大小和進度,則需要使用NSURLConnection。您可以獲取可用於監視進度的委託方法。 didSendBodyData:delegate方法告訴你有多少數據以字節爲單位。 connectionDidFinishLoading是你在你的NSXMLParser代碼中使用receivedData的地方。

NSURLRequest *theRequest = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0]; 
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 

    receivedData = [[NSMutableData data] retain]; 
} 

} 
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
// This method is called when the server has determined that it 
// has enough information to create the NSURLResponse. 

// It can be called multiple times, for example in the case of a 
// redirect, so each time we reset the data. 

// receivedData is an instance variable declared elsewhere. 
[receivedData setLength:0]; 
} 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
// release the connection, and the data object 

// inform the user 
NSLog(@"Connection failed! Error - %@ %@", 
     [error localizedDescription], 
     [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); 
[connection release]; 
// receivedData is declared as a method instance elsewhere 
[receivedData release]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
// Append the new data to receivedData. 
// receivedData is an instance variable declared elsewhere. 
[receivedData appendData:data]; 
} 
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{ 

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

感謝您的建議的當前狀態。我將不得不重新編寫一些代碼,但你已經把我引向了正確的方向。 獲取數據,然後解析...而不是解析數據進來! GOT IT! :) – Louie 2010-07-01 22:14:08

4
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [webData setLength: 0]; 
    CGFloat size = [[NSString stringWithFormat:@"%lli",[response expectedContentLength]] floatValue]; 
    NSLog(@"Size : %f",size); 

} 

上面的代碼會給你總規模

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 


    [webData appendData:data]; 
    totalDownloadedData += [data length]; // global integer 
    NSLog(@"Status : %d/%f",totalDownloadedData,size); 

} 

上面的代碼會顯示您下載

+0

夢幻般的解決方案......... – 2012-08-02 17:35:55

相關問題