zoukankan      html  css  js  c++  java
  • 网络解析 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);

        }];

    }

  • 相关阅读:
    解决:std::ostream operator<< should have been declared inside 'xxx'
    c++ friend 遇到 namespace 无法访问 private 成员的问题
    Compiler Error C2872: ambiguous symbol
    【持续更新】总结:C++开发时积累的一些零碎的东西
    陷阱:C++模块之间的”直接依赖“和”间接依赖“与Makefile的撰写
    ZThread::ThreadLocal:ERROR C4716 must return a value的解决
    java值传递
    iframe与父页面传值
    iframe父子兄弟之间调用传值(contentWindow && parent)
    MySQL返回影响行数的测试示例
  • 原文地址:https://www.cnblogs.com/haiying/p/4104635.html
Copyright © 2011-2022 走看看