zoukankan      html  css  js  c++  java
  • XMLDictionary 解析的使用

     XMLDictionary 解析的使用    
      (day16-1)-Xcode
    解析和生成XML提供了一个简单的方法,可方便地将XML文件转换为NSDictionary。可将任何字典的内容以XML形式输出。
    导入 包//  XMLDictionary.h//  XMLDictionary.m
    在viewController中导入 #import "XMLDictionary.h"
    当网址为http而非https
    记住在info中最后一项添加 App transport security setting 添加 allow —YES;
     
    代码1
    #import "ViewController.h"
    #import "XMLDictionary.h"
    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        获取文件
        NSString *path=[[NSBundle mainBundle]pathForResource:@"Myxml" ofType:@"xml"];
     将文件转化为data类型
        NSData *data=[NSData dataWithContentsOfFile:path];
     
    //输出文件以字典 方法:[NSDictionary dictionaryWithXMLFile:path].
     
        NSLog(@"%@",[NSDictionary dictionaryWithXMLFile:path]);
    }
     
    Get和POST请求
    从表面的意思看 get和post的区别:get是请求数据 post是发送数据 这是错误的 其实两者都可以的 。在iOS服务器发送请求里面可以带参数

    那么这些误区是怎么出现的呢?先看看一下对http的解释

    一般在浏览器中输入网址访问资源都是通过GET方式;在FORM提交中,可以通过Method指定提交方式为GET或者POST,默认为GET提交 
    Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE 
    URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP中的GET,POST,PUT,DELETE就对应着对这个资源的查 ,改 ,增 ,删 4个操作。到这里,大家应该有个大概的了解了,GET一般用于获取/查询 资源信息,而POST一般用于更新 资源信息(个人认为这是GET和POST的本质区别,也是协议设计者的本意,其它区别都是具体表现形式的差异 )。

    再进一步了解下他们两个的区别:

    1. GET使用URL或Cookie传参。而POST将数据放在BODY中。

    2. GET的URL会有长度上的限制,则POST的数据则可以非常大。

    3. POST比GET安全,因为数据在地址栏上不可见。

     
     
    //   get请求 路径
        NSString *path=@"http://1.studyios.sinaapp.com/gyxy.php?a=qq";
    //    创建请求对象
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:path]];
    //    1.创建一个会话对象
        NSURLSession *session=[NSURLSession sharedSession];

        //    2.通过请求信息,获取数据结构(根据会话创建一个task 发送请求)。
        //    completionHandler 回调 data响应体信息 response:响应头信息,主要是对服务器端的描述error:错误信息,如果请求失败,则error有值
        NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSArray *arr=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
            NSLog(@"%@",arr);
        }];
    //    执行任务
        [task resume];
    }
     
    代码2
    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController<UITextFieldDelegate>

    @property(strong,nonatomic) UITextField *txtInfo;
    @end
     
    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
     
        self.txtInfo=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 44)];
        self.txtInfo.borderStyle=1;
        self.txtInfo.delegate=self;
        [self.view addSubview:self.txtInfo];
     
    }

    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        NSString *path=[NSString stringWithFormat:@"http://1.studyios.sinaapp.com/gyxy.php?a=%@&b=bbb&c=ccc",self.txtInfo.text]
        ;
       
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:path]];
        //   1.创建一个会话对象
        NSURLSession *session=[NSURLSession sharedSession];
        //    2.通过请求信息 获取数据结构
        NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            //        NSLog(@"%@",data);
           
            NSArray *arr= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
           
            NSLog(@"%@",arr);
        }];
       
        [task resume];
        return YES;
    }
     

    使用NSURLSession发送POST请求的方法和NSURLConnection类似,整个过程如下:

    1)确定请求路径(一般由公司的后台开发人员以接口文档的方式提供)

    2)创建 可变的 请求对象(因为需要修改),此步骤不可以省略 

    3)修改请求方法为POST

    4)设置请求体,把参数转换为二进制数据并设置请求体

    5)创建会话对象(NSURLSession)

    6)根据会话对象创建请求任务(NSURLSessionDataTask)

     
    7)执行Task
    8)当得到服务器返回的响应后,解析数据(XML|JSON|HTTP)
     
     
    //  ViewController.m
    //  day16-2
    //
    //  Created by tlh on 16/3/28.
    //  Copyright © 2016年 lamco. All rights reserved.
    //

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
       
        NSString *path=@"http://1.studyios.sinaapp.com/mypost.php";
       
        NSURL *url=[NSURL URLWithString:path];
       
        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    //    post请求
        request.HTTPMethod=@"POST";
    //    参数
        NSString *param=[NSString stringWithFormat:@"can1=test&can2=test1"];
    //    将参数转换成data类型数据
        NSData *dataparam=[param dataUsingEncoding:NSUTF8StringEncoding];
    //    request的参数的主体
        request.HTTPBody=dataparam;
       
        NSURLSession *session=[NSURLSession sharedSession];
        NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]);
           
        }];
        [task resume];
    }
  • 相关阅读:
    .net 日期格式化
    grunt 上手
    设计模式的认识
    顺时针打印矩阵
    WCF 框架运行时类图
    Python闭包详解
    软件用了那些技术
    zoj 1610 Count the Colors(线段树延迟更新)
    快速提高自己的技术的办法?有两个方法
    纯win32实现PNG图片透明窗体
  • 原文地址:https://www.cnblogs.com/tianlianghong/p/5330955.html
Copyright © 2011-2022 走看看