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

0 0
原创粉丝点击