.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®ion=%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(@"连接失败");
}