xml地图|网站地图|网站标签 [设为首页] [加入收藏]

C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市

iOS使用UITableView从plist中选拔省市区

前几天在做三个气象应用,须求客户接收所在城市。思量到都市消息常常不会发生大的变动,所以从网络找到了中华城市音讯的xml文件,上边是应用二个tableview达成地域接收的代码,比较轻易,就不表达了。
AddressViewController.h文件

//
//  AddressViewController.h
//  AddressDemo
//
//  Created by worthy.zhang on 15/5/29.
//  Copyright (c) 2015年 worthy.zhang. All rights reserved.
//

#import 
#define kDisplayProvince 0
#define kDisplayCity 1
#define kDisplayArea 2
@interface AddressViewController : UIViewController
@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,assign)int displayType;
@property(nonatomic,strong)NSArray *provinces;
@property(nonatomic,strong)NSArray *citys;
@property(nonatomic,strong)NSArray *areas;
@property(nonatomic,strong)NSString *selectedProvince;//选中的省
@property(nonatomic,strong)NSString *selectedCity;//选中的市
@property(nonatomic,strong)NSString *selectedArea;//选中的区
@end

AddressViewController.m文件

//
//  AddressViewController.m
//  AddressDemo
//
//  Created by worthy.zhang on 15/5/29.
//  Copyright (c) 2015年 worthy.zhang. All rights reserved.
//

#import AddressViewController.h

@interface AddressViewController ()
@property(nonatomic,strong)NSIndexPath *selectedIndexPath;//当前选中的NSIndexPath
@end

@implementation AddressViewController

-(void)viewDidLoad{
    [self configureData];
    [self configureViews];

}

-(void)configureData{
    if (self.displayType == kDisplayProvince) {
        //从文件读取地址字典
        NSString *addressPath = [[NSBundle mainBundle] pathForResource:@address ofType:@plist];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithContentsOfFile:addressPath];
        self.provinces = [dict objectForKey:@address];
    }
}

-(void)configureViews{
    if (self.displayType == kDisplayProvince) { //只在选择省份页面显示取消按钮
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@取消 style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    }
    if (self.displayType == kDisplayArea) {//只在选择区域页面显示确定按钮
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@确定 style:UIBarButtonItemStylePlain target:self action:@selector(submit)];
    }
    CGRect frame = [self.view bounds];
    self.tableView = [[UITableView alloc]initWithFrame:frame];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.displayType == kDisplayProvince) {
        return self.provinces.count;
    }else if (self.displayType == kDisplayCity){
        return self.citys.count;
    }else{
        return self.areas.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* ID = @cityCell;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        if (self.displayType == kDisplayArea) {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }else{
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    if (self.displayType == kDisplayProvince) {
        NSDictionary *province = self.provinces[indexPath.row];
        NSString *provinceName = [province objectForKey:@name];
        cell.textLabel.text= provinceName;
    }else if (self.displayType == kDisplayCity){
        NSDictionary *city = self.citys[indexPath.row];
        NSString *cityName = [city objectForKey:@name];
        cell.textLabel.text= cityName;
    }else{
        cell.textLabel.text= self.areas[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:@unchecked];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.displayType == kDisplayProvince) {
        NSDictionary *province = self.provinces[indexPath.row];
        NSArray *citys = [province objectForKey:@sub];
        self.selectedProvince = [province objectForKey:@name];
        //构建下一级视图控制器
        AddressViewController *cityVC = [[AddressViewController alloc]init];
        cityVC.displayType = kDisplayCity;//显示模式为城市
        cityVC.citys = citys;
        cityVC.selectedProvince = self.selectedProvince;
        [self.navigationController pushViewController:cityVC animated:YES];
    }else if (self.displayType == kDisplayCity){
        NSDictionary *city = self.citys[indexPath.row];
        self.selectedCity = [city objectForKey:@name];
        NSArray *areas = [city objectForKey:@sub];
        //构建下一级视图控制器
        AddressViewController *areaVC = [[AddressViewController alloc]init];
        areaVC.displayType = kDisplayArea;//显示模式为区域
        areaVC.areas = areas;
        areaVC.selectedCity = self.selectedCity;
        areaVC.selectedProvince = self.selectedProvince;
        [self.navigationController pushViewController:areaVC animated:YES];
    }
    else{
        //取消上一次选定状态
        UITableViewCell *oldCell =  [tableView cellForRowAtIndexPath:self.selectedIndexPath];
        oldCell.imageView.image = [UIImage imageNamed:@unchecked];
        //勾选当前选定状态
        UITableViewCell *newCell =  [tableView cellForRowAtIndexPath:indexPath];
        newCell.imageView.image = [UIImage imageNamed:@checked];
        //保存
        self.selectedArea = self.areas[indexPath.row];
        self.selectedIndexPath = indexPath;
    }

}
-(void)submit{
    NSString *msg = [NSString stringWithFormat:@%@-%@-%@,self.selectedProvince,self.selectedCity,self.selectedArea];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@选择地址 message:msg delegate:nil cancelButtonTitle:@取消 otherButtonTitles:nil, nil];
    [alert show];
}

-(void)cancel{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

职能图如下:
图片 1
图片 2  

近年来在做一个气象应用,必要客户选用所在城市。思量到都市音讯日常不会生出大的转换,所以从互连网...

百度天气

  接口地址:

  获取到的多少是那般的:

{"error":0,"status":"success","date":"2014-10-27","results":[{"currentCity":"上海","pm25":"95","index":[{"title":"穿衣","zs":"较舒适","tipt":"穿衣指数","des":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"旅游","zs":"适宜","tipt":"旅游指数","des":"天气较好,温度适宜,但风稍微有点大。这样的天气适宜旅游,您可以尽情地享受大自然的无限风光。"},{"title":"感冒","zs":"较易发","tipt":"感冒指数","des":"天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"},{"title":"运动","zs":"较适宜","tipt":"运动指数","des":"天气较好,但风力较大,推荐您进行室内运动,若在户外运动请注意防风。"},{"title":"紫外线强度","zs":"弱","tipt":"紫外线强度指数","des":"紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"}],"weather_data":[{"date":"周一 10月27日 (实时:19℃)","dayPictureUrl":"http://api.map.baidu.comweather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.comweather/night/duoyun.png","weather":"多云","wind":"东北风3-4级","temperature":"21 ~ 16℃"},{"date":"周二","dayPictureUrl":"http://api.map.baidu.comweather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.comweather/night/yin.png","weather":"多云转阴","wind":"东风微风","temperature":"21 ~ 17℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.comweather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.comweather/night/xiaoyu.png","weather":"小雨","wind":"东风微风","temperature":"21 ~ 19℃"},{"date":"周四","dayPictureUrl":"http://api.map.baidu.comweather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.comweather/night/xiaoyu.png","weather":"小雨","wind":"东南风微风","temperature":"23 ~ 20℃"}]}]}

  根据重回的Json定义出相应的数据构造:

public class BaiduTQ
    {
        public int error { get; set; }
        public string status { get; set; }
        public string date { get; set; }
        public List<BaiduResult> results { get; set; }
    }

    public class BaiduResult
    {
        public string currentCity { get; set; }
        public string pm25 { get; set; }
        public List<BaiduIndex> index { get; set; }
        public List<BaiDuWeaterData> weather_data { get; set; }
    }

    public class BaiduIndex
    {
        public string title { get; set; }
        public string zs { get; set; }
        public string tipt { get; set; }
        public string des { get; set; }
    }

    public class BaiDuWeaterData
    {
        public string date { get; set; }
        public string dayPictureUrl { get; set; }
        public string nightPictureUrl { get; set; }
        public string weather { get; set; }
        public string wind { get; set; }
        public string temperature { get; set; }
    }

  然后一向通过Newtonsoft.Json 反连串化成就能够。

  既然是获得天气,鲜明是愿意拿到客商所在城市的天气,下一步则是索要根据顾客机器IP获取所在城市,然后拿走该城市的气象音讯。

IP获取城市

  通过Tmall的IP库,

  有了上面包车型客车路子,大家下一步的做事正是获得顾客的外网IP,而外网IP,是机器连接外网才会有,所以楼主写了八个页面,布署在外网服务器。

  相关代码如下:

         var ip = Request.UserHostAddress;
            using (var client = new WebClient())
            {
                var url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip;
                client.Encoding = Encoding.UTF8;
                var str = client.DownloadString(url);
                Response.Write(str);
            }

  那样我们就足以获得到顾客所在城市的天气数据了。

本文由永利澳门平台发布于计算机资讯,转载请注明出处:C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市

您可能还会对下面的文章感兴趣: