2014-09-01 57 views
0

我需要從服務器url下載html文件並替換爲本地html文件。我正在使用AFNetworking下載文件並存儲到Documents文件夾。它正在下載視頻&音頻文件。但是,當我嘗試下載HTML文件我得到以下錯誤Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x83d0730 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}iOS從url下載html文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"solarhtml"]; 

if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; 

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" 
forHTTPHeaderField:@"Content-Type"]; 

[manager setResponseSerializer:[AFJSONResponseSerializer serializer]]; 
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil]; 


[manager GET:@"http://server.net/projects/index.html" 
parameters:nil 
success:^(AFHTTPRequestOperation *operation, id responseObject) { 
[operation.responseData writeToFile:[dataPath stringByAppendingPathComponent:@"index.html"] atomically:YES]; 


NSLog(@"Successfully downloaded file to %@", [NSURL fileURLWithPath:dataPath]); 
NSLog(@"THE RESPONSE: %@", responseObject); 


} 
failure:^(AFHTTPRequestOperation *operation, NSError *error1) { 
NSLog(@"%@", error1); 
}]; 

訪問HTML文件:

-(void)estimatesavings:(id)sender{ 


     if(updateclick==YES){ 

     web_estimate=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024, 768)]; 

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"index.html"]; 

     NSURL *targetURL = [NSURL fileURLWithPath:filePath]; 
     NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; 
     [web_estimate loadRequest:request]; 

     web_estimate.delegate=self; 

     [self.view addSubview:web_estimate]; 

    }else{ 


    NSString *pathToBundle = [[NSBundle mainBundle] bundlePath]; 
    NSURL *baseURL = [NSURL fileURLWithPath:pathToBundle]; 
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; 
    NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
    //CGRect fullScreenRect=[[UIScreen mainScreen]applicationFrame]; 
    web_estimate=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024, 768)]; 
    [web_estimate loadHTMLString:htmlString baseURL:baseURL]; 



    web_estimate.delegate=self; 
    [self.view addSubview:web_estimate]; 

    } 


    } 

錯誤:

copy failed: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x83c8b50 {NSSourceFilePathErrorKey=/Users/ranganathagv/Library/Application Support/iPhone Simulator/6.1/Applications/CEDFEBEB-2A5C-40A9-8965-761689FD83C2/ActewAGL.app/index.html, NSUserStringVariant=(
    Copy 
), NSFilePath=/Users/ranganathagv/Library/Application Support/iPhone Simulator/6.1/Applications/CEDFEBEB-2A5C-40A9-8965-761689FD83C2/ActewAGL.app/index.html, NSDestinationFilePath=/Users/ranganathagv/Library/Application Support/iPhone Simulator/6.1/Applications/CEDFEBEB-2A5C-40A9-8965-761689FD83C2/Documents/solarhtml/index.html, NSUnderlyingError=0x83c89c0 "The operation couldn’t be completed. No such file or directory"}c 
+0

如果您的答案不是JSON,爲什麼您使用JSON的'AFJSONResponseSerializer'? – Larme 2014-09-01 08:17:59

+0

但我使用相同的從互聯網下載.mp4文件。它的工作..但爲什麼它不適用於HTML文件? – user3743552 2014-09-01 08:26:14

+0

它的工作..但它並沒有取代我的本地html文件。成功將文件下載到文件://localhost/Users/ranganathagv/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/0DFCC406-FAF9-4F12-9F25-3EAE12CF7B9F/Documents/solarhtml/ – user3743552 2014-09-01 08:34:48

回答

0

有兩個問題:

  1. 嘗試使用AFJSONResponseSerializer並更改acceptableContentTypes將不起作用,因爲AFJSONResponseSerializer將接受響應,但仍會嘗試解析JSON。相反,您應該只使用AFHTTPResponseSerializer。有關更多信息,請參閱https://stackoverflow.com/a/21621530/1271826

  2. 另一個問題在於你打開的例程。而不是隻從包中打開文件,您應該打開文件夾中的文件。您甚至可能希望將該文件從該文件夾複製到Documents文件夾(以防尚未下載)。

    例如:

    NSString *documentsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 
    NSString *solarFolder  = [documentsFolder stringByAppendingPathComponent:@"solarhtml"]; 
    NSString *documentsPath = [solarFolder stringByAppendingPathComponent:@"index.html"]; 
    NSString *bundlePath  = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; 
    
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    
    if (![fileManager fileExistsAtPath:documentsPath]) { 
        NSError *error; 
        if (![fileManager fileExistsAtPath:solarFolder]) { 
         if (![fileManager createDirectoryAtPath:solarFolder withIntermediateDirectories:YES attributes:nil error:&error]) { 
          NSLog(@"create folder failed: %@", error); 
         } 
        } 
        if (![fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&error]) { 
         NSLog(@"copy failed: %@", error); 
        } 
    } 
    
    NSString *htmlString = [NSString stringWithContentsOfFile:documentsPath encoding:NSUTF8StringEncoding error:nil]; 
    web_estimate=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024, 768)]; 
    [web_estimate loadHTMLString:htmlString baseURL:baseURL]; 
    

    這樣,如果該文件尚未在所有下載,這將文件從包試圖在必要時打開它,之前複製。

+0

這是給錯誤..檢查我編輯的代碼 – user3743552 2014-09-01 09:45:49

+0

下載前不加載本地html文件.. – user3743552 2014-09-01 09:47:15

+0

您是否創建了'solarhtml'文件夾?我調整了我的代碼示例以說明如何做到這一點。你也希望類似的「如果'solarhtml'文件夾沒有,在下載例程中創建它」邏輯。 – Rob 2014-09-01 10:01:21