一个简单的天气预报


封装的网络请求

#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;

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值