iOS编程——异步GET请求

.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController<NSURLConnectionDelegate>
{
    
}
@property (nonatomic,strong)NSMutableData  * receiveData;
@end

.m
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.backgroundColor = [UIColor redColor];
    btn.frame = CGRectMake(80, 80, 160, 30);
    [self.view addSubview:btn];
    [btn setTitle:@"GET" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(getRequest) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)getRequest
{
    NSString *urlStr=@"http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C®ion=%E6%B5%8E%E5%8D%97&output=json&ak=6E823f587c95f0148c19993539b99295";
    NSURL * url = [NSURL URLWithString:urlStr];
    NSURLRequest * requset = [NSURLRequest requestWithURL:url];
    NSURLConnection * connection = [[NSURLConnection alloc]initWithRequest:requset delegate:self];
    
}


//服务器收到响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"服务器收到响应");
    self.receiveData=[NSMutableData dataWithCapacity:1];
}
//传输数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"传输数据");
    [_receiveData appendData:data];
    
}
//传输完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *respondseStr=[[NSString alloc] initWithData:_receiveData encoding:NSUTF8StringEncoding];
    // JSON 解析
    //   NSLog(@"%@",dic);
    NSLog(@"resong %@",respondseStr);
    
}


//连接失败
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"连接失败");
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
从代码中可以看出最重要的就是UIButton中的四句

1.需要一个NSString 的字符串连接

NSString *urlStr=@"http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C&region=%E6%B5%8E%E5%8D%97&output=json&ak=6E823f587c95f0148c19993539b99295";

2.用这个字符串初始化一个NSURL

    NSURL * url = [NSURL URLWithString:urlStr];

3.用url初始化NSURLRequest 

    NSURLRequest * requset = [NSURLRequest requestWithURL:url];

4.初始化NSURLConnection

    NSURLConnection * connection = [[NSURLConnection alloc]initWithRequest:requset delegate:self];

5.实现NSURLConnection中的代理方法;并定义一个可变Data来接受请求下来的数据。

//服务器收到响应

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    NSLog(@"服务器收到响应");

    self.receiveData=[NSMutableData dataWithCapacity:1];

}

//传输数据

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    NSLog(@"传输数据");

    [_receiveData appendData:data];

    

}

//传输完成

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{


    NSString *respondseStr=[[NSString alloc] initWithData:_receiveData encoding:NSUTF8StringEncoding];

    // JSON 解析

    //   NSLog(@"%@",dic);

    NSLog(@"resong %@",respondseStr);

    

}



//连接失败

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    NSLog(@"连接失败");

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值