网络数据请求

<span style="font-family: Arial, Helvetica, sans-serif;">       NSString *strURL = @"http://api.map.baidu.com/place/v2/search?query=银行&region=大连&output=json&ak=6E823f587c95f0148c19993539b99295";</span>
    // 一个正常的URL地址不允许有中文,只有26个英文字母的大小写,数字和一些特殊符号如$,%,如果遇到带中文的URL地址,首先要进行编码
    NSString *strEncode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"%@", strEncode);
    // URL地址符合要求后,开始进行网络请求
    // 1.跟据已经编码的URL创建一个NSURL
    NSURL *url = [NSURL URLWithString:strEncode];
    // 2.发送一个请求
    NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:url];
    // 3.返回我们要的数据,一个NSDdata对象
    // 第一个参数是请求
    // 第二个参数是返回的一个响应
    // 第三个参数是错误信息
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    // 解析数据
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *array = dic[@"results"];
    for (NSDictionary *dic in array) {
        NSLog(@"%@", dic[@"name"]);
// 异步GET请求

NSString *strURL = @"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg";
    NSURL *url = [NSURL URLWithString:strURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 通过代理的方式进行异步操作
    [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    // 只要接收到服务器返回的响应信息,就会走这个方法,我们在这个方法里需要对接收数据的容器data进行初始化设置
    self.data = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // 只要返回数据,就会走这个协议方法
    // append是累加的意思
    [self.data appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // 整个请求结束,需要把返回的data对imgView的image进行赋值
    self.imgView.image = [UIImage imageWithData:self.data];
}
// 异步POST请求

NSString *strURL = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
    // 1.创建一个URL
    NSURL *url = [NSURL URLWithString:strURL];
    // 2.创建一个request请求
    NSMutableURLRequest *reuqest = [NSMutableURLRequest requestWithURL:url];
    // 3.设置request请求方式
    [reuqest setHTTPMethod:@"POST"];
    // 4.创建body字符串
    NSString *bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";
    // 5.将body字符创转换成data
    NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    // 6.将body放到request中
    [reuqest setHTTPBody:data];
    // 7.
    // 网络请求在子线程里进行请求,请求下来的数据通过控件作为载体实现出来,需要把数据在子线程里显示,第二个参数就是指定把数据返回到哪个线程
    [NSURLConnection sendAsynchronousRequest:reuqest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        // 参数的data就是请求下来的数据,解析就在block中进行
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = dic[@"news"];
        for (NSDictionary *dic in array) {
            NSLog(@"%@", dic[@"title"]);
        }
    }];

// 异步GET通过block方式

NSString *str = @"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg";
    // 1.创建url
    NSURL *url = [NSURL URLWithString:str];
    // 2.发送请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 3.异步
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        self.imgView.image = [UIImage imageWithData:data];
    }];

// 同步GET请求

NSString *strURL = @"http://api.map.baidu.com/place/v2/search?query=银行&region=大连&output=json&ak=6E823f587c95f0148c19993539b99295";
    // 一个正常的URL地址不允许有中文,只有26个英文字母的大小写,数字和一些特殊符号如$,%,如果遇到带中文的URL地址,首先要进行编码
    NSString *strEncode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"%@", strEncode);
    // URL地址符合要求后,开始进行网络请求
    // 1.跟据已经编码的URL创建一个NSURL
    NSURL *url = [NSURL URLWithString:strEncode];
    // 2.发送一个请求
    NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:url];
    // 3.返回我们要的数据,一个NSDdata对象
    // 第一个参数是请求
    // 第二个参数是返回的一个响应
    // 第三个参数是错误信息
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    // 解析数据
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *array = dic[@"results"];
    for (NSDictionary *dic in array) {
        NSLog(@"%@", dic[@"name"]);
// 同步POST请求

NSString *strURL = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
    NSURL *url = [NSURL URLWithString:strURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 把请求方式设置成POST,默认是GET
    [request setHTTPMethod:@"POST"];
    // 把请求的内容放到request的body中
    NSString *bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";
    // 需要把请求部分的字符串编程NSData类型的对象
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    // 把bodyData放到request中
    [request setHTTPBody:bodyData];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    // json解析
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *array = dic[@"news"];
    for (NSDictionary *dic in array) {
        NSLog(@"%@", dic[@"title"]);






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值