URL 和JSON和 plist小结
来源:互联网 发布:mac弱点 编辑:程序博客网 时间:2024/05/26 13:00
#import "URL.h"
@interface URL ()
@property(nonatomic,strong) UIWebView *webView;
@end
@implementation URL
(void)viewDidLoad {
[super viewDidLoad];self.webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:self.webView];
[self plistDemo];
}
(void)demo2 {
//创建统一资源定位符
NSURL *url = [NSURL URLWithString:@”http://192.168.199.20/tex.text“];//创建网络请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
/*
NSURLRequestUseProtocolCachePolicy = 0, 默认缓存
NSURLRequestReloadIgnoringLocalCacheData = 1, 忽略本地缓存直接加载服务器数据NSURLRequestReturnCacheDataElseLoad = 2, 先走缓存数据,然后走服务器数据NSURLRequestReturnCacheDataDontLoad = 3, 返回缓存数据没如果没有缓存就空着
*/
//cachePolicy 缓存策略 timeoutInterval请求超时时长NSURLRequest *request1 = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:15.0];
// NSBlockOperation *q = [NSBlockOperation blockOperationWithBlock:^{
//
// NSLog(@”dddd”);
//
// }];
NSOperationQueue *aa = [[NSOperationQueue alloc]init];//如果是aa就是异步 如果是[NSThread currentThread];就是主线程
// [aa addOperation:q];
/*NSURLResponseURL 服务器返回的URL 大部分一样有时候会不一样“重定向”MIMEType 二进制文件类型 服务器会告诉客户端可以用什么软件打开expectedContentLength 下载的文件长度textEncodingName 文本编码名称,大多是UTF8suggestedFilename 服务器建议保存的文件名称statusCode 状态码 2XX 3XX 重定向 4XX客户端错误 5XX服务器错误allHeaderFields 所以响应头的内容 data: 数据内容 就是要展示的数据 connectionError:如果没错错误也没有数据 应该是网络错误*///发送网络请求[NSURLConnection sendAsynchronousRequest:request queue:aa completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { NSLog(@"%@",[NSThread currentThread]); NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]); /* JSON对象具有以下属性 1.顶节点是一个NSArray或NSDictionary - Top level object is an NSArray or NSDictionary 所有对象必须是NSString, NSNumber, NSArray, NSDictionary, or NSNull - All objects are NSString, NSNumber, NSArray, NSDictionary, or NSNull 字典的key必须是NSStrings - All dictionary keys are NSStrings NSNumbers不能是无穷大 或空 - NSNumbers are not NaN or infinity */ /* options 位移枚举 NSJSONReadingMutableContainers 容器节点是可变的 NSJSONReadingMutableLeaves 子节点是可变的 NSJSONReadingAllowFragments 允许顶级节点是是数组或字典 */ id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"%@",obj); NSLog(@"%@",[obj class]); for (NSDictionary *dict in obj) { NSLog(@"%@",dict[@"firstName"]); }}];NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];NSLog(@"%@",data);NSLog(@"ssss");[NSURLSession sharedSession];
}
-(void)plistDemo{
//创建统一资源定位符NSURL *url = [NSURL URLWithString:@"http://192.168.199.20/Info.plist"];//创建网络请求NSURLRequest *request = [NSURLRequest requestWithURL:url];//发送网络请求[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { NSLog(@"%@",[NSThread currentThread]); NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]); /* options枚举 NSPropertyListImmutable 不可变容器默认 NSPropertyListMutableContainers 可变的解析完之后 NSPropertyListMutableContainersAndLeaves 容器和子节点可变的 */ id obj = [NSPropertyListSerialization propertyListWithData:data options:0 format:NULL error:NULL]; NSLog(@"%@",obj[@"CFBundleDevelopmentRegion"]); NSLog(@"%@",[obj class]);}];
}
(void)demo1{
//url
NSURL *url = [NSURL URLWithString:@”https://m.baidu.com“];//建立请求,
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];//告诉服务器一些附加信息
[request setValue:@”iPthone AppleWebKit” forHTTPHeaderField:@”User-Agent”];//建立网络连接
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {NSLog(@"%@",data);//存到本地[data writeToFile:@"/Users/xiaojie/Desktop/txt" atomically:YES];//将字符串转换成htmlNSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];[self.webView loadHTMLString:html baseURL:nil];
}];
}
@end
- URL 和JSON和 plist小结
- 12.Swift JSON 和 PList
- 关于JSON和plist和XML之间的转换
- JSON和plist的序列化和反序列化
- iis7.5 plist和json加载出错处理
- 本地文件的获取和解析(json,plist)
- iOS 读取json文件和读取plist文件
- 加载plist和json在UILabel换行需求中的区别
- itunesmetadata.plist 和 softwareVersionExternalIdentifier
- plist写入和读取
- plist和NSUserdefaults存储
- plist解析和JSON序列化与反序列化(JSON解析),XML解析
- ajax和json与php小结合
- 全国城市数据库plist文件和sqlite文件和json文件
- CocoStudio生成的json和plist还有合成的png的读法问题解决方法
- JSON解析详解(JSONKit、苹果自带解析)和PList文件的反序列化
- OC语言笔记(七):OC字符串Json、plist、NSNumber和NSValue、KVC
- 使用coffeescript定时任务下载服务器xml和plist文件解析为json
- ubuntu使用chkconfig命令来管理服务
- 我的2017
- 安卓自定义布局
- 装箱(boxing)和拆箱(unboxing) [转]
- PowerDesigner(一)-PowerDesigner概述(系统分析与建模)
- URL 和JSON和 plist小结
- C/C++经典源代码网站
- C++和C运行库
- mac 下 profile文件
- Object Pascal语言与Delphi什么关系?
- Android客制化------根据hdmi状态来显示触摸圆点
- eclipse安装
- Supervisor 使用简介
- Jquery中的DOM操作 (一.查找和创建节点)