使用ASIHTTPRequest获得天气信息
2012-10-07 10:48
387 查看
使用ASIHTTPRequest+JSON一个简单的天气获取Demo,直接上代码:
工程结构截图如下:
![](http://blog.csdn.net/jwzbskywz/article/details/7701437)
加入ASIHTTPRequest的方法请查看上一篇文件,ViewController.h代码如下:
ViewController.m如下:
工程结构截图如下:
加入ASIHTTPRequest的方法请查看上一篇文件,ViewController.h代码如下:
1 // 2 // ViewController.h 3 // ASIHTTPRequestDemo 4 // 5 // Created by Fox on 12-3-13. 6 // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface ViewController : UIViewController 12 { 13 NSDictionary *cityWeather; 14 15 IBOutlet UILabel *city; 16 IBOutlet UILabel *weather; 17 } 18 19 - (IBAction)getWeather:(id)sender; 20 -(void)showWeather; 21 @property(nonatomic, retain) NSDictionary *cityWeather; 22 23 @end
ViewController.m如下:
1 // 2 // ViewController.m 3 // ASIHTTPRequestDemo 4 // 5 // Created by Fox on 12-3-13. 6 // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "JSON.h" 11 #import "ASIHTTPRequest.h" 12 13 @interface ViewController () 14 15 @end 16 17 @implementation ViewController 18 19 @synthesize cityWeather; 20 21 - (void)viewDidLoad 22 { 23 [super viewDidLoad]; 24 // Do any additional setup after loading the view, typically from a nib. 25 } 26 27 - (void)viewDidUnload 28 { 29 [city release]; 30 city = nil; 31 [weather release]; 32 weather = nil; 33 [super viewDidUnload]; 34 // Release any retained subviews of the main view. 35 } 36 37 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 38 { 39 return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 40 } 41 42 - (void)dealloc { 43 [city release]; 44 [weather release]; 45 [super dealloc]; 46 } 47 48 - (IBAction)getWeather:(id)sender { 49 50 //use the opensourse ASIHTTPRequest to work with a network 51 //get a city's weather 52 NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101270101.html"]; 53 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 54 [request setDelegate:self]; 55 [request startAsynchronous]; 56 57 58 } 59 60 -(void)showWeather{ 61 [city setText:[NSString stringWithFormat:@"%@",[cityWeather objectForKey:@"city"]]]; 62 63 // NSLog(@"cityName is %@",[cityWeather objectForKey:@"city"]); 64 65 [weather setText:[NSString stringWithFormat:@"%@",[cityWeather objectForKey:@"weather2"]]]; 66 67 } 68 69 //call this method when request finished 70 - (void)requestFinished:(ASIHTTPRequest *)request 71 { 72 // NSLog(@"the network is OK..."); 73 74 SBJSON *parser = [[SBJSON alloc] init]; 75 76 // Use when fetching text data 77 NSString *responseString = [request responseString]; 78 // NSLog(@"the responseString is %@",responseString); 79 80 // Use when fetching binary data 81 NSData *responseData = [request responseData]; 82 83 // NSLog(@"the responseData is %@",responseData); 84 85 NSDictionary *dic = [[NSDictionary alloc] initWithDictionary:[parser objectWithString:responseString error:nil]]; 86 87 88 self.cityWeather= [dic objectForKey:@"weatherinfo"]; 89 90 91 [self showWeather]; 92 93 [parser release]; 94 } 95 96 97 - (void)requestFailed:(ASIHTTPRequest *)request 98 { 99 NSError *error = [request error]; 100 NSLog(@"the error is %@",error); 101 } 102 103 104 @end
相关文章推荐
- 使用ASIHTTPRequest获得天气信息
- 使用ASIHTTPRequest获得天气信息
- iOS代码技巧之ASIHttpRequest类库的使用及Json解析(以解析中国天气网天气预报为例)
- iOS开发asi使用(二)ASIHttpRequest发送数据
- ASIHTTPRequest类库简介和使用说明
- ASIHttpRequest使用详解
- 使用HttpWebRequest提交信息
- ASIHTTPRequest 使用HTTPS
- ASIHTTPRequest类库简介和使用说明
- ASIHTTPRequest使用
- ASIHTTPRequest类库简介和使用说明
- ASIHTTPRequest框架使用总结系列之阿堂教程4(下载数据)
- 如何使用ASIHTTPRequest来进行网络请求的
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- 使用ASIHTTPRequest实现下载功能
- iOS开发ASIHTTPRequest中Cookie的使用
- ASIHTTPRequest-Cookie的使用
- ASIHTTPRequest类库简介和使用说明
- 关于loadrunner使用web_add_header添加HTTP信息头(比如Content-Type,token等)和使用web_custom_request()提交json数据