AFN下载json文件

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文件)