封装的网络请求
#import "DataService.h"
#define BASE_URL @"http://www.weather.com.cn"
@implementation DataService
//在异步线程里面不需要返回值,但需要传入参数
+ (void)requestData:(NSString *)urlString
comletionHandle:(void(^)(id result))block
{
//构建完整的URL
NSString *requestURL = [BASE_URLstringByAppendingString:urlString];
NSURL *url = [NSURLURLWithString:requestURL];
//构建request对象
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"GET"];//也可以在方法里面传入参数来请求网络的方法
//发送网络请求
NSOperationQueue *queue = [[NSOperationQueuealloc] init];
[NSURLConnectionsendAsynchronousRequest:request
queue:queue
completionHandler:^(NSURLResponse *response,NSData *data, NSError *connectionError) {
//请求网络出错
if (connectionError) {
NSLog(@"error:%@", connectionError);
return;
}
//请求网络成功
id result = [NSJSONSerializationJSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
//请求网络成功后,将请求到的结果通过调用block传出去,要在主线程中调用
dispatch_async(dispatch_get_main_queue(), ^{
block(result);
});
}];
}
#import "ViewController.h"
#import "DataService.h"
@interface ViewController ()<NSURLConnectionDataDelegate>
{
NSDictionary *cityDic;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_pickerView.delegate =self;
_pickerView.dataSource =self;
[self_loadData];
}
- (void)_loadData
{
//找到json路径
NSString *filePath = [[NSBundlemainBundle] pathForResource:@"cityCode"ofType:@"plist"];
cityDic =[[NSDictionaryalloc] initWithContentsOfFile:filePath];
_cityArr = [cityDicallKeys];
// NSLog(@"cityArr is %@", _cityArr);
}
- (IBAction)btnAction:(UIButton *)sender {
_activity.hidden =NO;
//取得城市Id,以免第一个不能查询
//通过这种方法取得选定的行,而不用覆写协议方法
NSInteger selectedRow = [_pickerViewselectedRowInComponent:0];
NSString *selectedCity = [_cityArrobjectAtIndex:selectedRow];
NSString *cityID = [cityDicobjectForKey:selectedCity];
//调用封装的网络请求
//构建子URL
NSString *urlStr = [NSStringstringWithFormat:@"/data/sk/%@.html", cityID];
[DataServicerequestData:urlStr
comletionHandle:^(id result) {
//刷新UI
[selfrefreshUI:result];
//加载完成后隐藏风火轮
_activity.hidden =YES;
}];
}
//刷新UI
- (void)refreshUI:(NSDictionary *)result
{
//取得weather数据
NSDictionary *weatherDic = [result objectForKey:@"weatherinfo"];
self.cityLabel.text = [weatherDicobjectForKey:@"city"];
self.tempLabel.text = [weatherDicobjectForKey:@"temp"];
self.wdLabel.text = [weatherDicobjectForKey:@"WD"];
self.wsLabel.text = [weatherDicobjectForKey:@"WS"];
self.waterLabel.text = [weatherDicobjectForKey:@"SD"];
self.timeLabel.text = [weatherDicobjectForKey:@"time"];
}
#pragma mark -UIPickerViewDataSourceU
//这是两个必须实现的协议方法
//返回有几个pickerview
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
//返回有几行数据
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return_cityArr.count;
}
//这是非必须实现的协议方法
//设置行内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_cityArrobjectAtIndex:row];
}
#pragma mark -UIPickerViewDelegate
//设置行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 40;
}