加载数据

- (void)loadData{

// 获得网络管理者

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

// 要下载文件的url

NSURL *url = [NSURL URLWithString:@"http://app.fxingw.com/app/index/list.json"];

// 创建请求对象

NSURLRequest *request = [NSURLRequest requestWithURL:url];

// 异步

[[manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

// 告诉服务器下载的文本保存的位置在那里

NSString *homeDictionary = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];//获取根目录

//添加储存的文件名

NSURL *documentsDirectoryURL = [NSURL URLWithString:homeDictionary];

NSLog(@"file = %@",targetPath);

return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];

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

NSLog(@"response = %@,filePath = %@",response,filePath);

[self loadInfo:filePath.absoluteString];

}] resume];

}

- (void)loadInfo:(NSString *)path{

// 加载json文件路径

// 读取json文件到一个NSData对象

NSData *data = [NSData dataWithContentsOfFile:path];

// 将data转换成一个NSDictionary

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

// 全局 数组

_huandeng = [NSArray arrayWithArray:dict[@"huandeng"]];

_news = [NSArray arrayWithArray:dict[@"newslist"]];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容