-2

こんにちは、iPhone初心者です。nsurlconnection と json パーサーを使用してリモート サーバーからデータを取得しています。サーバーからファイルを 1 つだけダウンロードし、ドキュメント パスに保存しています。しかし、私のサーバーのURLには、(画像、オーディオ、ビデオ、テキストファイル)のようなファイル数があります。アプリ起動時に一括ダウンロードしてドキュメントディレクトリに保存する方法。また、サーバー内のファイル名と同じファイル名がドキュメント内に必要です。

私はこれらの方法を試しました。

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

}

私を助けてください何が間違っているのですか。

4

1 に答える 1

-1

申し訳ありませんが、あなたの質問をよく読むことができません。私が見たところ、AFNetworking ( https://github.com/AFNetworking/AFNetworking )を見るとメリットがあるかもしれません。ダウンロードのプロセスを簡素化し、堅牢です。

素敵なチュートリアルについては、こちらをご覧ください: http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_afnetworking/

于 2013-02-19T07:09:16.007 に答える