您的位置:首页 > 移动开发 > Objective-C

IOS Objective c 异步链接 网上下载内容到本地及SBjson解析

2015-11-15 17:12 381 查看



第一步:建另一个类,名为NetTest





第二步:代码

AppDelegate.h,AppDelegate.m,ViewController.h,main.m,都不变。

以下红字部分是添加代码。

1、ViewController.m

#import "ViewController.h"

#import "NetTest.h"

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)jkj:(id)sender {

NetTest *ss=[[NetTest alloc]init];

[self presentModalViewController:ss animated:YES];

}//“请进入”按钮拽如函数

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)viewDidUnload

{

[super viewDidUnload];

// Release any retained subviews of the main view.

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

@end

2、ViewController.xib





3、NetTest.h

#import

@interface NetTest : UIViewController//协议

@property(retain,nonatomic)NSMutableData *completData;//表示接受到的全部数据

@property (retain, nonatomic) IBOutlet UIButton *nest;

@end

4、NetTest.m

#import "NetTest.h"

#import "JSON.h"//导入json解析文件

@interface NetTest ()

@end

@implementation NetTest

@synthesize nest;

@synthesize completData;

//以下是协议对应的三个协议方法

//连接接受响应,表示成功建立连接

//(NSURLConnection *)connection表示连接对象

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

{

NSLog(@"连接成功");

self.completData=[[NSMutableData alloc]init];//创建对象

}

//连接接受数据

//(NSData *)data形参接受回来的数据

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

{

NSLog(@"连接接收到数据了");

[self.completData appendData:data];//反复的向可变数据添加数据

}

//连接完成

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

NSLog(@"连接完成了");

NSString *str=[[NSString alloc]initWithData:self.completData encoding:NSUTF8StringEncoding];

NSLog(@"str=%@",str);

NSDictionary *dic=[str JSONValue];//将字符串xml转化为字典

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

NSArray *jsonArray=[dic objectForKey:@"items"];//key值items对应的value是数组

NSDictionary *dic2=[jsonArray objectAtIndex:0];

NSLog(@"%@",dic2);

NSString *city=[dic2 objectForKey:@"city"];

NSLog(@"%@",city);

}

- (IBAction)yibulianjie:(id)sender {

//创建url对象

NSURL *url=[NSURL URLWithString:@"http://api.jiepang.com/v1/locations/search?lat=39.916&lon=116.393&count=2&source=100000"];

NSURLRequest *req=[NSURLRequest requestWithURL:url];//创建请求对象

[NSURLConnection connectionWithRequest:req delegate:self];//自己为代理

NSLog(@"连接发起");

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return self;

}

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view from its nib.

}

- (void)viewDidUnload

{

[self setNest:nil];

[super viewDidUnload];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

- (void)dealloc {

[nest release];

[super dealloc];

}

@end

5、NetTest.xib




第三步:运行

1、运行

2、点击“请进入”





3、点击“go”





结果为:2013-01-31 18:38:26.137 第九课 课堂老师作业[6853:c07] 连接发起

2013-01-31 18:38:27.314 第九课 课堂老师作业[6853:c07] 连接成功

2013-01-31 18:38:27.315 第九课 课堂老师作业[6853:c07] 连接接收到数据了

2013-01-31 18:38:27.315 第九课 课堂老师作业[6853:c07] 连接完成了

2013-01-31 18:38:27.315 第九课 课堂老师作业[6853:c07] str={"has_more": true, "items": [{"city": "\u5317\u4eac", "mayor_description": null, "tel": "010-65132255", "addr": "\u5317\u4eac\u4e1c\u57ce\u533a\u666f\u5c71\u524d\u88574\u53f7", "num_checkin_users": 12751, "venue_info":
{"rating": 8.8, "average": 133.6}, "lon": 116.397259593, "has_surprise": false, "has_mayor_coupon": false, "link": "http://jie.pn/l/WaWCC", "lat": 39.9129302145, "dist": "550 m", "guid": "4678579AE706BD3D", "has_event": false, "categories": [{"is_primary":
1, "id": "0406", "name": "\u535a\u7269\u9986/\u5c55\u89c8\u9986", "icon": "/static/img/categories/public/museum.gif"}], "name": "\u6545\u5bab\u535a\u7269\u9662(\u6545\u5bab)"}, {"city": "\u5317\u4eac", "mayor_description": null, "tel": "", "addr": "\u5317\u4eac\u6545\u5bab",
"num_checkin_users": 977, "venue_info": {"rating": 9.0, "average": 10.5}, "lon": 116.397060081718, "has_surprise": false, "has_mayor_coupon": false, "link": "http://jie.pn/l/hRmXc", "lat": 39.9172094562577, "dist": "200 m", "guid": "87F17C24FA3068A21C65D6B51923D3AE",
"has_event": false, "categories": [{"is_primary": 1, "id": "0418", "name": "\u5176\u4ed6", "icon": "/static/img/categories/public/default.gif"}, {"is_primary": 0, "id": "0518", "name": "\u5176\u4ed6", "icon": "/static/img/categories/public/outdoor.gif"}],
"name": "\u592a\u548c\u6bbf"}]}

2013-01-31 18:38:27.374 第九课 课堂老师作业[6853:c07] dic={

"has_more" = 1;

items = (

{

addr = "\U5317\U4eac\U4e1c\U57ce\U533a\U666f\U5c71\U524d\U88574\U53f7";

categories = (

{

icon = "/static/img/categories/public/museum.gif";

id = 0406;

"is_primary" = 1;

name = "\U535a\U7269\U9986/\U5c55\U89c8\U9986";

}

);

city = "\U5317\U4eac";

dist = "550 m";

guid = 4678579AE706BD3D;

"has_event" = 0;

"has_mayor_coupon" = 0;

"has_surprise" = 0;

lat = "39.9129302145";

link = "http://jie.pn/l/WaWCC";

lon = "116.397259593";

"mayor_description" = "";

name = "\U6545\U5bab\U535a\U7269\U9662(\U6545\U5bab)";

"num_checkin_users" = 12751;

tel = "010-65132255";

"venue_info" = {

average = "133.6";

rating = "8.8";

};

},

{

addr = "\U5317\U4eac\U6545\U5bab";

categories = (

{

icon = "/static/img/categories/public/default.gif";

id = 0418;

"is_primary" = 1;

name = "\U5176\U4ed6";

},

{

icon = "/static/img/categories/public/outdoor.gif";

id = 0518;

"is_primary" = 0;

name = "\U5176\U4ed6";

}

);

city = "\U5317\U4eac";

dist = "200 m";

guid = 87F17C24FA3068A21C65D6B51923D3AE;

"has_event" = 0;

"has_mayor_coupon" = 0;

"has_surprise" = 0;

lat = "39.9172094562577";

link = "http://jie.pn/l/hRmXc";

lon = "116.397060081718";

"mayor_description" = "";

name = "\U592a\U548c\U6bbf";

"num_checkin_users" = 977;

tel = "";

"venue_info" = {

average = "10.5";

rating = 9;

};

}

);

] {

addr = "\U5317\U4eac\U4e1c\U57ce\U533a\U666f\U5c71\U524d\U88574\U53f7";

categories = (

{

icon = "/static/img/categories/public/museum.gif";

id = 0406;

"is_primary" = 1;

name = "\U535a\U7269\U9986/\U5c55\U89c8\U9986";

}

);

city = "\U5317\U4eac";

dist = "550 m";

guid = 4678579AE706BD3D;

"has_event" = 0;

"has_mayor_coupon" = 0;

"has_surprise" = 0;

lat = "39.9129302145";

link = "http://jie.pn/l/WaWCC";

lon = "116.397259593";

"mayor_description" = "";

name = "\U6545\U5bab\U535a\U7269\U9662(\U6545\U5bab)";

"num_checkin_users" = 12751;

tel = "010-65132255";

"venue_info" = {

average = "133.6";

rating = "8.8";

};

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: