URL 和JSON和 plist小結

#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];

/*
NSURLResponse

URL                     服務器返回的URL 大部分一樣有時候會不一樣“重定向”
MIMEType                二進制文件類型 服務器會告訴客戶端可以用什麼軟件打開
expectedContentLength   下載的文件長度
textEncodingName        文本編碼名稱,大多是UTF8
suggestedFilename       服務器建議保存的文件名稱
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];
    
    //將字符串轉換成html
    NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    [self.webView loadHTMLString:html baseURL:nil];
    

    }];

}

@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章