-2
大家好,我是iPhone的新手。我使用nsurlconnection和json解析器从远程服务器检索数据。我只从服务器下载一个文件,并将其存储在文档路径中。但在我的服务器网址数量的文件有像(图像,音频,视频,文本文件)。如何在应用程序午餐时下载并将其保存在文档目录中。而且我还希望文件中的文件名与服务器中的文件名相同。如何从远程服务器中检索多媒体数据并使用目标存储文档目录c
我已经试过这些方法。
ViewController
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSMutableData *responseData;
NSArray *filesCount;
}
@property(nonatomic,retain)NSArray *filesCount;
@property(nonatomic,retain) NSMutableData *responseData;
@end
.m viewController
#import "ViewController.h"
#import "JSON/JSON.h"
@interface ViewController()
@end
@implementation ViewController
@synthesize filesCount,responseData;
- (void)viewDidLoad
{
[super viewDidLoad];
responseData =[[NSMutableData data]retain];
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://XXXXXXX/XXXXX/filesCount.php"]];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"DidFailWithError" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSLog(@"response string is %@",responseString);
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
filesCount = [json objectWithString:responseString error:&error];
[responseString release];
NSLog(@"filesCount is %@",filesCount);
if (filesCount==nil) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Json parsing failed" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[alert show];
}
else{
NSMutableString *text = [NSMutableString stringWithString:@"\n"];
for (int i = 0; i < [filesCount count]; i++)
[text appendFormat:@"%@\n", [filesCount objectAtIndex:i]];
NSLog(@"text is %s",[text UTF8String]);
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:text ]]];
NSData *addImageData = UIImagePNGRepresentation(img);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSRange lastComma= [text rangeOfString:@"/" options:NSBackwardsSearch];
NSString *requiredSubString = [text substringFromIndex:(lastComma.location+1)];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:requiredSubString];
[fileManager createFileAtPath:savedImagePath contents:addImageData attributes:nil];
NSLog(@"Saved Document dir %@",savedImagePath);
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"" message:@"files are downloaded" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[alert1 show];
}
}
请帮助我什么错我疯了。
@ .Richard Brown.The链接是好的,但我需要下载图像的文件夹。需要文档中的文件名与服务器中的文件名相同。请帮帮我 – user2085991 2013-02-19 07:29:32