使用block回调实现GET异步网络请求
1、创建网络地址,需要传入数据的接口。
2、通过网址创建请求对象
3、设置请求方式(这里是GET)
4、使用链接,发送请求,获取数据
5、处理数据
看看代码是怎么实现的
// 1.网络地址
NSURL *url = [NSURL URLWithString:BASE_URL_1];
// 2.通过网址创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 2.1.设置请求方式
[request setHTTPMethod:@"GET"];
// 3.使用链接,发送请求,获取数据
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:
^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", dict);
}];
BASE_URL_1是宏定义
#define BASE_URL_1 @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/
APINewsList.ashx?date=20150624&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"
这样就可以在网络上获取到数据了,然后处理返回来的data,在页面上显示
这里给大家提供一个对外开放接口的网站:http://apistore.baidu.com/非常实用的网站