网络解析 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);

    }];

}

原文地址:https://www.cnblogs.com/haiying/p/4104635.html