美文网首页
AFN下载json文件

AFN下载json文件

作者: liang_1 | 来源:发表于2017-04-11 10:51 被阅读0次

    AFN下载文件 存到沙盒并读取json

    直接上代码 

    AFHTTPSessionManager *manage  = [AFHTTPSessionManager manager];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://xxxxxxxxxx/json/data.json"]];

    NSURLSessionDownloadTask *downloadTask = [manage downloadTaskWithRequest:request progress:nil destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

               NSString *caches = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/"]];

              NSString *fullpath = [caches stringByAppendingPathComponent:response.suggestedFilename];

             NSURL *filePathUrl = [NSURL fileURLWithPath:fullpath];

             return filePathUrl;

    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nonnull filePath, NSError * _Nonnull error) {

            NSLog(@"文件下载完毕---%@",filePath);

           NSData *data = [NSData dataWithContentsOfFile:    [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"type_all.json"]];

                id JsonObject=[NSJSONSerialization JSONObjectWithData:data

                                                              options:NSJSONReadingAllowFragments

                                                               error:nil];

    //        NSStringEncoding gbkEncoding = 

    // CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

      //     NSString *pageSource = [[NSString alloc] initWithData:data encoding:gbkEncoding];

    //       NSData *newData=[pageSource dataUsingEncoding:NSUTF8StringEncoding];

    //        self.allTypeArr = [NSJSONSerialization JSONObjectWithData:newData  options:NSJSONReadingMutableContainers error:nil];

    self.alltypeArr = JsonObject ;

            NSLog(@"===%@",self.alltypeArr);

    }];

    [downloadTask resume];

    相关文章

      网友评论

          本文标题:AFN下载json文件

          本文链接:https://www.haomeiwen.com/subject/dryyattx.html