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

} 

請幫助我什麼錯我瘋了。

回答

-1

對不起,我真的不能很好地讀你的問題。從我看到的,儘管你可能從AFNetworking(https://github.com/AFNetworking/AFNetworking)中獲益。它簡化了下載過程,並且堅如磐石。

在這裏尋找一個很好的教程:http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_afnetworking/

+0

@ .Richard Brown.The鏈接是好的,但我需要下載圖像的文件夾。需要文檔中的文件名與服務器中的文件名相同。請幫幫我 – user2085991 2013-02-19 07:29:32

相關問題