iOS开发:GET与POST接口网络请求以及对AFNetworking的二次封装

本文介绍了iOS开发中GET和POST网络请求的两种常见封装方法,并详细讲解了如何使用AFNetworking进行网络请求的管理,包括设置响应内容类型、GET和POST请求的实现。通过示例代码展示了如何进行数据解析和错误处理。
摘要由CSDN通过智能技术生成

/***************     iOS开发:GET   和   POST接口网络请求        *******************/

-、网络请求的两种方式:GET   和   POST

1.普通封装方法:封装到一个类中

1、自定义一个block

typedefvoid (^DataBlock) (id data);

2、自定义GET 、POST方法

+ (void)getDataByURLString:(NSString *)urlString WithDataBlock:(DataBlock)dataBlock;


+ (void)getDataByURLString:(NSString *)urlString HttpMethod:(NSString *)method BodyString:(NSString *)bodyString DataBlock:(DataBlock)datablock;

3、实现GET、POST方法

+ (void)getDataByURLString:(NSString *)urlString WithDataBlock:( DataBlock)dataBlock{


    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

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

       

        dataBlock(data);

        

    }];

}


+ (void)getDataByURLString:(NSString *)urlString HttpMethod:(NSString *)method BodyString:(NSString *)bodyString DataBlock:(DataBlock)datablock{

    

    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//默认是get方法,修改成post

    if ([method isEqual:@"POST"]) {

        [request setHTTPMethod:method];

        NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];

        [request setHTTPBody:bodyData];

    }

    

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

       

        datablock(data);

    

    }];

}

在ViewController中直接调用类(+)方法:

get请求:

[HttpMethod getDataByURLString:@"http://c.3g.163.com/nc/article/headline/T1348647853363/0-140.html" WithDataBlock:^(id data) {

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

        NSLog(@"%@", dic); //此处就可以对数据进行具体解析

    }];


post请求:

 [HttpMethod getDataByURLString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"  HttpMethod:@"POST" BodyString:@"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213" DataBlock:^(id data) {

       

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


        NSLog(@"%@", dic);//此处就可以对数据进行具体解析

        

    }];

    

二、AFNetWorking封装:

    

(1).下载并引入AFNetWorking头文件:
直接在viewController中:

   a.创建AF管理对象:

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    

    b.设置网址(接口请求数据)符合的类型:

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/json", @"application/json", @"text/javascript", @"text/html", nil];

    c.进行数据解析:

get方法

    [manager GET:@"http://c.3g.163.com/nc/article/headline/T1348647853363/0-140.html" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        

        NSLog(@"%@", responseObject);

        

           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"请求失败,请检查网络"); //此处就可以对数据进行具体解析

    }];

    post方法:

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"20131129", @"date", @"1", @"startRecord", @"30", @"len", @"1234567890", @"udid", @"Iphone", @"terminalType", @"213", @"cid", nil];


    [manager GET:@"http://c.3g.163.com/nc/article/headline/T1348647853363/0-140.html" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {

       

        NSLog(@"%@", responseObject); //此处就可以对数据进行具体解析

        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"请求失败,请检查网络");

        

    }];



 







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值