解析前获取URL文件大小

时间:2010-07-01 21:06:27

标签: iphone iphone-sdk-3.0

我了!我使用NSURL获取文件然后解析它。我一直在寻找几个小时的进度条我试图在我的应用程序中实现。我知道我首先需要获取文件大小,然后继续更新我已经下载的数据量。我看过使用“ASIHTTPRequest”的例子,但有没有办法用我已有的方法做到这一点?

这是我开始下载的地方。

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

}

有人能指出我如何获取文件大小的正确方向,然后如何更新我下载的数量。提前谢谢!

2 个答案:

答案 0 :(得分: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);

}

以上代码将向您显示下载的当前状态

答案 1 :(得分:2)

如果要获取文件大小和进度,则需要使用NSURLConnection。您将获得可用于监视进度的委托方法。 didSendBodyData:delegate方法告诉您有多少数据以字节为单位。 connectionDidFinishLoading是您在NSXMLParser代码中使用receiveData的地方。

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{