• 网络解析 get 和post


    //get同步

    - (IBAction)getT:(id)sender {

        //准备一个Url

        NSURL *url=[NSURL URLWithString:BASE_URL];

        //创建一个请求对象

        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];

        //选择请求方式

        [request setHTTPMethod:@"GET"];

        //创建响应对象

        NSURLResponse *response=nil;

        //是否出错

        NSError *error=nil;

        //创建连接

        NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        //解析数据

       NSArray*arr=[NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];

        //转换成字符串

        NSString *s=[NSString stringWithFormat:@"%@",arr];

        //打印

        NSLog(@"%@",s);

    }

     *******************************************

    //POST  同步

    - (IBAction)postT:(id)sender {

          //准备一个url

        NSURL *url=[NSURL URLWithString:BASE_URL_2];

        //创建一个请求对象

        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];

        [request setHTTPMethod:@"POST"];

        

        //body

        NSData *databody=[BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding];

        //给请求设置body

        [request setHTTPBody:databody];

          //创建连接

        NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

        //数据解析

        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];

         NSString *s=[NSString stringWithFormat:@"%@",dic];

        NSLog(@"%@",s);

    }

     *****************************

     

    //GET 异步  代理

    - (IBAction)getYBDL:(id)sender {

        //准备 URl

        NSURL *url=[NSURL URLWithString:BASE_URL];

        //创建请求对象

        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];

        [request setHTTPMethod:@"GET"];

        //创建链接(同时设置代理)

      NSURLConnection*conn=[NSURLConnection connectionWithRequest:request delegate:self];

        //启动链接

        [conn start];

    }

    //代理方法一 :接收到响应

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

    {

        self.redata=[NSMutableData data];

    }

    //代理方法二:接收数据

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

    {

        [self.redata appendData:data];

    }

    //代理方法三:接收完成处理数据

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection

    {

       //数据处理

        NSArray *arr=[NSJSONSerialization JSONObjectWithData:self.redata options:(NSJSONReadingMutableContainers) error:nil];

        NSString *s=[NSString stringWithFormat:@"%@",arr];

        NSLog(@"%@",s);

        

    }

    //代理方法四:出错

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

    {

        

    }

     ******************************************************

    //POST异步 block

    - (IBAction)postBLOCK:(id)sender {

        //准备url

        NSURL *url=[NSURL URLWithString:BASE_URL_2];

        //创建请求对象

        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];

        [request setHTTPMethod:@"POST"];

        //加body

        NSData *databody=[BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding];

        [request setHTTPBody:databody];

        //创建连接

        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

           //解析数据

            NSArray *arr=[NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];

            //转换数据

            NSString *s=[NSString stringWithFormat:@"%@",arr];

           //打印

            NSLog(@"%@",s);

        }];

     }

    ***************************************************

    //GET异步 block

    - (IBAction)getBLOCK:(id)sender {

        NSURL *url=[NSURL URLWithString:BASE_URL];

        NSURLRequest *request=[NSURLRequest requestWithURL:url];

        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

            NSArray *arr=[NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];

            

            NSString *s=[NSString stringWithFormat:@"%@",arr];

            NSLog(@"%@",s);

        }];

    }

  • 相关阅读:
    网页元素居中的n种方法
    Swifter.Json 可能是 .Net 平台迄今为止性能最佳的 Json 序列化库【开源】
    .NET 欢乐编程术之类型超级转换之术👍👍
    C#.Net 使用 JsonReader/JsonWriter 高性能解析/生成 Json 文档
    UTF-16 -- 顶级程序员也会忽略的系统编码问题,JDK 错了十年!
    迄今为止 .Net 平台功能最强大,性能最佳的 JSON 序列化和反序列化库。
    并发系列(一)——线程池源码(ThreadPoolExecutor类)简析
    Flink源码阅读(一)——Per-job之Yarn的作业调度(一)
    阅读GitHub源码的正确打开方式
    安装Elasticsearch+Kibana【单节点、多ES实例】
  • 原文地址:https://www.cnblogs.com/haiying/p/4104635.html
Copyright © 2020-2023  润新知