Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS从url下载html文件_Html_Ios_Json_Afnetworking - Fatal编程技术网

iOS从url下载html文件

iOS从url下载html文件,html,ios,json,afnetworking,Html,Ios,Json,Afnetworking,我需要从服务器url下载html文件并替换为本地html文件。我正在使用AFNetworking下载文件并存储到Documents文件夹。它正在下载视频和音频文件。但是,当我尝试下载html文件时,我会看到下面的错误:errorerror Domain=NSCocoaErrorDomain code=3840“操作无法完成。(Cocoa错误3840)。“(JSON文本没有以数组或对象开头,并且不允许设置片段的选项。)UserInfo=0x83d0730{NSDebugDescription=JS

我需要从服务器url下载html文件并替换为本地html文件。我正在使用AFNetworking下载文件并存储到Documents文件夹。它正在下载视频和音频文件。但是,当我尝试下载html文件时,我会看到下面的错误:error
error Domain=NSCocoaErrorDomain code=3840“操作无法完成。(Cocoa错误3840)。“(JSON文本没有以数组或对象开头,并且不允许设置片段的选项。)UserInfo=0x83d0730{NSDebugDescription=JSON文本没有以数组或对象开头,并且没有设置允许片段的选项。}

 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

有两个问题:

  • 尝试使用
    AFJSONResponseSerializer
    并更改
    acceptableContentTypes
    将不起作用,因为
    AFJSONResponseSerializer
    将接受响应,但仍将尝试解析JSON。相反,您应该只使用
    AFHTTPResponseSerializer
    。有关更多信息,请参阅

  • 另一个问题在于您的打开例程。您不应该只是从捆绑包中打开文件,而应该在“文档”文件夹中打开文件。您甚至可能希望将文件从捆绑包复制到“文档”文件夹(以防尚未下载)

    例如:

    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];
    
    这样,如果文件根本没有下载,这将在尝试打开文件包之前从包中复制文件(如果需要)


  • 如果您的答案不是JSON,为什么要使用
    AFJSONResponseSerializer
    作为JSON?但我使用相同的方法从internet下载.mp4文件。它可以工作..但为什么它不适用于html文件?它可以工作..但它不会替换我的本地html文件。已成功将文件下载到file://localhost/Users/ranganathagv/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/0DFCC406-FAF9-4F12-9F25-3EAE12CF7B9F/Documents/solarhtml/无需AFNetwork为此,请尝试stringWithContentsOfURL:NSString*content=[NSString stringWithContentsOfURL:[NSURL URLWithString:requestString]编码:NSUTF8StringEncoding错误:error];@user3743552很难协调“它正在工作”和“它没有替换我的本地html文件”。这到底是哪里出了问题?你在那个位置看过那个文件了吗?实际的文件内容有变吗(即,问题是您无法再次加载本地副本)?还是文件没有更改(即,问题是缓存还是保存失败)?必须是其中之一。(顺便说一句,感谢您删除重复的问题!)您创建了
    solarhtml
    文件夹了吗?我调整了我的代码示例,以说明如何创建。您还希望在下载例程中使用类似的“如果
    solarhtml
    文件夹没有,创建它”逻辑。