【问题标题】:downloading using NSURLConnection not downloading anything?使用 NSURLConnection 下载不下载任何东西?
【发布时间】:2012-05-09 04:49:22
【问题描述】:

我正在使用 NSURLConnection 从服务器下载 mp3 数据,我的代码在这里

- (IBAction)downloadData:(id)sender
 {

   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
   NSURL *url = [[NSURL alloc] initWithString:@"http://viadj.viastreaming.net/start/psalmsmedia/ondemand/Nin%20snehamethrayo.mp3"];

   [request setURL:url];
   [url release];
   url = nil;

   NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];


 }

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
 {
     responseData = [[NSMutableData alloc] init];
 }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
 {
   [responseData appendData:data];
 }

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
 {
  [responseData release];
  [connection release];

 }

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

  NSLog(@"Succeeded! Received %d bytes of data",[responseData
                                               length]);
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"];

  [responseData writeToFile:fileName atomically:YES];

  responseData = nil;
  self->imageConnection = nil;


  }

我对下载的路径有点困惑。当我单击下载按钮时,我显示“成功!收到 1329 字节的数据”,但没有下载任何内容。需要一些帮助。我们将如何指定 iPhone 的本地路径来存储下载的数据?

【问题讨论】:

标签: iphone objective-c path download nsurlconnection


【解决方案1】:
- (IBAction)downloadData:(id)sender
{
  NSURL *url = [[NSURL alloc] initWithString:@"http://viadj.viastreaming.net/start/psalmsmedia/ondemand/Nin%20snehamethrayo.mp3"];
  NSMutableURLRequest   *theRequest_to = [NSMutableURLRequest requestWithURL:url];
  [url release];
  NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest_to delegate:self];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{
  NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"snehamethrayo.mp3"]; // here you can set your filename which you can get from url 
  [[NSFileManager defaultManager] createFileAtPath:filepath contents:nil attributes:nil];
  file = [[NSFileHandle fileHandleForUpdatingAtPath:filepath] retain];// Here file is object of NSFileHandle and its declare in .h File
  [file seekToEndOfFile];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  [file seekToEndOfFile];
  [file writeData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection*)connection 
{
 [file closeFile];
}

【讨论】:

  • 感谢您的反馈。但是,这些数据保存在哪里。我在 didReceiveData 之后打印了数据并显示了一些二进制值。我们是否需要对其进行解码,或者我们将如何查看正在下载的数据?
  • 当我在 ma 设备上运行此代码时,文件路径显示为 /var/mobile/Applications/939A18E2-0CAC-4FD4-A5A9-01BA34680256/Documents/snehamethrayo.mp3 ,实际上该文件保存在哪里,iPhone本地文件?我在我的设备上看不到下载的文件
【解决方案2】:

我认为不需要任何代码更改。只需放置一个 nslog 并查看...

NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"];
NSLog(@"%@",fileName);

这将像这样列出文件位置 /Users/me/Library/Application Support/iPhone Simulator/5.0/Applications/(你的应用)/Documents/myFile.即下载的文件在您的文件夹中。

注意:不要忘记把文件格式ie

NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile.mp3"];

【讨论】:

  • 他正在下载一个 9.7 MB 磁盘上的 mp3 文件
  • @Hector for exaple 我放了那个链接,通常我所有的 mp3 文件都是这个大小(9/8.something),所以我应该使用什么方法,这更有帮助。以及如何显示下载进度?
  • @Akhildas 当我在 ma 设备上运行此代码时,文件路径显示为 /var/mobile/Applications/939A18E2-0CAC-4FD4-A5A9-01BA34680256/Documents/snehamethrayo.mp3 ,实际上这是在哪里文件已保存,iPhone 本地文件?我在我的设备上看不到下载的文件
  • @NeerajNeeru 要查看您的文件,请在您的 .plist 文件中启用 应用程序支持 iTunes 文件共享。然后你可以在 iTunes 中看到文件
  • @Akhildas 对不起,我只是一个学习者.. 是的,我编辑了 plist 以支持 iTunes 文件共享。但是我可以添加进度视图以查看下载开始/结束的内容吗?
猜你喜欢
  • 2021-03-07
  • 2017-06-06
  • 2017-08-22
  • 2021-09-19
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
相关资源
最近更新 更多