天气预报---网络加载最新天气信息,截取JSON文件

仿普通的手机上的天气预报功能,从网络上截取JSON文件

实现的效果图如下:

具体代码如下:

FirstViewController.m文件

#import "FirstViewController.h"

#import "JRViewController.h"

#import "CityModel.h"

#import "JSONKit.h"

#define kW self.view.frame.size.width

#define kH self.view.frame.size.height

@interface
FirstViewController ()

//当前城市

@property (nonatomic,copy)
NSString * cityName;

//当前日期

@property (nonatomic,strong)
NSArray * dateArray;

//当前天气小图片

@property (nonatomic,strong)
NSArray * imageViewArray;

//当前温度label

@property (nonatomic,strong)
NSArray * temperArray;

//天气详情介绍

@property (nonatomic,strong)
NSArray * introArray;

//右上角,当前温度

@property (nonatomic,copy)
NSString * rightTemper;

//右上角,天气情况

@property (nonatomic,copy)
NSString * rightIntro;

//晒衣指数

@property (nonatomic,copy)
NSString * dress;

//紫外线指数

@property (nonatomic,copy)
NSString * purple;

//洗车指数

@property (nonatomic,copy)
NSString * car;

//人体舒适程度

@property (nonatomic,copy)
NSString * comfort;

@end

@implementation FirstViewController

- (void)viewDidLoad {

[super
viewDidLoad];

self.view.backgroundColor=[UIColor
whiteColor];

UIImageView * imageView=[[UIImageView
alloc]initWithImage:[UIImage
imageNamed:@"DuoYun"]];

imageView.frame=self.view.bounds;

[self.view
addSubview:imageView];

self.title=@"天气预报";

//加载烟台的天气

[self
_loadYT];

//加载视图

[self
_loadView];

}

#pragma mark - 加载视图

- (void) _loadView

{

//右上角当天温度情况

UILabel * leftLabel=[[UILabel
alloc]initWithFrame:CGRectMake(200,
60, kW-200,
50)];

//leftLabel.backgroundColor=[UIColor redColor];

leftLabel.textAlignment=NSTextAlignmentCenter;

leftLabel.text=_rightTemper;

[leftLabel
setFont:[UIFont
fontWithName:nil
size:30]];

leftLabel.textColor=[UIColor
whiteColor];

[self.view
addSubview:leftLabel];

//右上角,天气情况

UILabel * rightIntroLabel=[[UILabel
alloc]initWithFrame:CGRectMake(200,
100, kW-200,
40)];

//rightIntroLabel.backgroundColor=[UIColor greenColor];

rightIntroLabel.text=_rightIntro;

[rightIntroLabel
setTextColor:[UIColor
blueColor]];

rightIntroLabel.textAlignment=NSTextAlignmentCenter;

[self.view
addSubview:rightIntroLabel];

//当前城市

UILabel * RightCityLabel=[[UILabel
alloc]initWithFrame:CGRectMake(0,
70, 150,
70)];

//RightCityLabel.backgroundColor=[UIColor greenColor];

[RightCityLabel
setTextColor:[UIColor
whiteColor]];

RightCityLabel.textAlignment=NSTextAlignmentCenter;

RightCityLabel.text=_cityName;

[RightCityLabel
setFont:[UIFont
fontWithName:nil
size:40]];

[self.view
addSubview:RightCityLabel];

//切换城市按钮

UIButton * changeCityButton=[[UIButton
alloc]initWithFrame:CGRectMake((kW-100)/2,
kH-50,
100, 45)];

changeCityButton.backgroundColor=[UIColor
purpleColor];

[changeCityButton setTitle:@"切换城市"
forState:UIControlStateNormal];

[changeCityButton setTitleColor:[UIColor
whiteColor] forState:UIControlStateNormal];

[changeCityButton addTarget:self
action:@selector(changeCity)
forControlEvents:UIControlEventTouchUpInside];

[self.view
addSubview:changeCityButton];

//下方多日天气视图

for (int i=0; i<4; i++)

{

UIView * view=[[UIView
alloc]initWithFrame:CGRectMake(((kW-4*80)/5)*(i+1)+i*80,
kH-290,
80, 230)];

view.backgroundColor=[UIColor
colorWithRed:0.1
green:0.1
blue:0.1
alpha:0.1];

//date

UILabel * dateLabel=[[UILabel
alloc]initWithFrame:CGRectMake(0,
0, 80,
50)];

dateLabel.backgroundColor=[UIColor
colorWithRed:0.1
green:0.1
blue:0.1 alpha:0.2];

dateLabel.text=_dateArray[i];

dateLabel.textAlignment=NSTextAlignmentCenter;

[view
addSubview:dateLabel];

//image

UIImageView * imageView=[[UIImageView
alloc]initWithFrame:CGRectMake(0,
55, 80,
80)];

//imageView.backgroundColor=[UIColor greenColor];

NSURL * url=[NSURL
URLWithString:_imageViewArray[i]];

NSData * data=[NSData
dataWithContentsOfURL:url];

UIImage * image=[UIImage
imageWithData:data];

imageView.image=image;

[view
addSubview:imageView];

//天气情况简介label

UILabel * intrLabel=[[UILabel
alloc]initWithFrame:CGRectMake(0,
130, 80,
60)];

//intrLabel.backgroundColor=[UIColor redColor];

intrLabel.textAlignment=NSTextAlignmentCenter;

intrLabel.adjustsFontSizeToFitWidth=YES;

intrLabel.text=_introArray[i];

[view
addSubview:intrLabel];

//温度label

UILabel * tempery=[[UILabel
alloc]initWithFrame:CGRectMake(0,
190, 80,
40)];

tempery.backgroundColor=[UIColor
colorWithRed:0.1
green:0.1
blue:0.1 alpha:0.2];

tempery.text=_temperArray[i];

tempery.textAlignment=NSTextAlignmentCenter;

[tempery
setFont:[UIFont
fontWithName:nil
size:15]];

[view
addSubview:tempery];

[self.view
addSubview:view];

//中间比较大的视图

UIView * middleView=[[UIView
alloc]initWithFrame:CGRectMake(50,
140, kW-100,
225)];

middleView.backgroundColor=[UIColor
colorWithRed:0.1
green:0.1
blue:0.1 alpha:0.05];

//晒衣指数

UILabel * dressLabel=[[UILabel
alloc]initWithFrame:CGRectMake(50,
10, 100,
40)];

dressLabel.backgroundColor=[UIColor
colorWithRed:0.1
green:0.1
blue:0.1 alpha:0.06];

dressLabel.text=@"晒衣指数";

dressLabel.textAlignment=NSTextAlignmentCenter;

dressLabel.textColor=[UIColor
whiteColor];

[middleView
addSubview:dressLabel];

UILabel * RdressLabel=[[UILabel
alloc]initWithFrame:CGRectMake(180,
5, 250,
50)];

//RdressLabel.backgroundColor=[UIColor redColor];

RdressLabel.text=_dress;

[RdressLabel
setTextColor:[UIColor
whiteColor]];

[middleView
addSubview:RdressLabel];

//紫外线指数

UILabel * purpleLabel=[[UILabel
alloc]initWithFrame:CGRectMake(50,
65, 100,40)];

purpleLabel.backgroundColor=[UIColor
colorWithRed:0.1
green:0.1
blue:0.1 alpha:0.06];

purpleLabel.text=@"紫外线指数";

purpleLabel.textAlignment=NSTextAlignmentCenter;

purpleLabel.textColor=[UIColor
whiteColor];

[middleView
addSubview: purpleLabel];

UILabel * RpurpleLabel=[[UILabel
alloc]initWithFrame:CGRectMake(180,
10+50,
250,50)];

//RpurpleLabel.backgroundColor=[UIColor redColor];

RpurpleLabel.text=_purple;

[RpurpleLabel
setTextColor:[UIColor
whiteColor]];

[middleView
addSubview: RpurpleLabel];

//洗车指数

UILabel * carLabel=[[UILabel
alloc]initWithFrame:CGRectMake(50,
65+40+15,
100, 40)];

carLabel.backgroundColor=[UIColor
colorWithRed:0.1
green:0.1
blue:0.1 alpha:0.06];

carLabel.text=@"洗车指数";

carLabel.textAlignment=NSTextAlignmentCenter;

carLabel.textColor=[UIColor
whiteColor];

[middleView
addSubview:carLabel];

UILabel * RcarLabel=[[UILabel
alloc]initWithFrame:CGRectMake(180,
65+50,
250, 50)];

//RcarLabel.backgroundColor=[UIColor redColor];

RcarLabel.text=_car;

[RcarLabel
setTextColor:[UIColor
whiteColor]];

[middleView
addSubview:RcarLabel];

//人体舒适程度

UILabel * personComfortLabel=[[UILabel
alloc]initWithFrame:CGRectMake(50,
115+50+5,
100, 40)];

personComfortLabel.backgroundColor=[UIColor
colorWithRed:0.1
green:0.1
blue:0.1 alpha:0.06];

personComfortLabel.text=@"人体舒适程度";

personComfortLabel.adjustsFontSizeToFitWidth=YES;

personComfortLabel.textAlignment=NSTextAlignmentCenter;

personComfortLabel.textColor=[UIColor
whiteColor];

[middleView
addSubview:personComfortLabel];

UILabel * RpersonComfortLabel=[[UILabel
alloc]initWithFrame:CGRectMake(180,
115+50,
250, 50)];

//RpersonComfortLabel.backgroundColor=[UIColor redColor];

RpersonComfortLabel.text=_comfort;

[RpersonComfortLabel
setTextColor:[UIColor
whiteColor]];

[middleView
addSubview:RpersonComfortLabel];

[self.view
addSubview:middleView];

}

}

#pragma mark - 加载烟台的天气

- (void) _loadYT

{

NSURL * url=[NSURL
URLWithString:@"http://m.weather.com.cn/atad/101120501.html"];

NSData * data=[NSData
dataWithContentsOfURL:url];

NSDictionary * dic = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingMutableContainers
error:nil];

NSDictionary * tempDic=dic[@"weatherinfo"];

//城市名

NSString * cityName=tempDic[@"city"];

_cityName=cityName;

//日期数组

NSDate * date1=[NSDate
date];

NSDate * date2=[NSDate
dateWithTimeIntervalSinceNow:60*60*24];

NSDate * date3=[NSDate
dateWithTimeIntervalSinceNow:60*60*24*2];

NSDate * date4=[NSDate
dateWithTimeIntervalSinceNow:60*60*24*3];

NSDateFormatter * format=[[NSDateFormatter
alloc]init];

[format
setDateFormat:@"MM月dd日"];

NSString * str1=[format
stringFromDate:date1];

NSString * str2=[format
stringFromDate:date2];

NSString * str3=[format
stringFromDate:date3];

NSString * str4=[format
stringFromDate:date4];

_dateArray=@[str1,str2,str3,str4];

//温度数组

NSString * temper1=tempDic[@"temp1"];

NSString * temper2=tempDic[@"temp2"];

NSString * temper3=tempDic[@"temp3"];

NSString * temper4=tempDic[@"temp4"];

_temperArray=@[temper1,temper2,temper3,temper4];

//天气小图标数组---存放图片地址

NSString * string1=tempDic[@"img1"];

NSInteger index1=[string1
integerValue];

NSString * imageSrc1=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";

imageSrc1=[NSString
stringWithFormat:imageSrc1,index1];

//NSLog(@"%@",imageSrc1);

NSString * string2=tempDic[@"img3"];

NSInteger index2=[string2
integerValue];

NSString * imageSrc2=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";

imageSrc2=[NSString
stringWithFormat:imageSrc2,index2];

//NSLog(@"%@",imageSrc2);

NSString * string3=tempDic[@"img5"];

NSInteger index3=[string3
integerValue];

NSString * imageSrc3=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";

imageSrc3=[NSString
stringWithFormat:imageSrc3,index3];

//NSLog(@"%@",imageSrc3);

NSString * string4=tempDic[@"img7"];

NSInteger index4=[string4
integerValue];

NSString * imageSrc4=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";

imageSrc4=[NSString
stringWithFormat:imageSrc4,index4];

//NSLog(@"%@",imageSrc4);

_imageViewArray=@[imageSrc1,imageSrc2,imageSrc3,imageSrc4];

//天气详情数组

NSString * intr1=tempDic[@"weather1"];

NSString * intr2=tempDic[@"weather2"];

NSString * intr3=tempDic[@"weather3"];

NSString * intr4=tempDic[@"weather4"];

_introArray=@[intr1,intr2,intr3,intr4];

//右上角当天的温度情况

_rightTemper=tempDic[@"temp1"];

//右上角当天天气情况

_rightIntro=tempDic[@"weather1"];

//晾衣指数

_dress=tempDic[@"index_ls"];

//紫外线指数

_purple=tempDic[@"index_uv"];

//洗车指数

_car=tempDic[@"index_xc"];

//人体舒适程度

_comfort=tempDic[@"index_co"];

}

- (void) _loadData

{

NSString * path=[[NSBundle
mainBundle] pathForResource:@"weather_city_code.json"
ofType:nil];

NSData * data=[NSData
dataWithContentsOfFile:path];

NSDictionary * dic=[NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingMutableContainers
error:nil];

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

NSArray * array=dic[@"城市代码"];

//NSLog(@"%@",array);

//创建model

CityModel * citymodel=[[CityModel
alloc]init];

for (int i=0; i<array.count; i++)

{

NSDictionary * tempDic=array[i];

//NSLog(@"%@",tempDic);

citymodel.provinceName=tempDic[@"省"]; 
  //获取到各省

//NSLog(@"%@",citymodel.provinceName);

NSArray * cityArray=tempDic[@"市"];

for(NSDictionary * cityDic
in cityArray)

{

NSLog(@"%@",cityDic);

//            _cityArray=cityDic[@"市名"];

//            _codeArray=cityDic[@"编码"];

}

}

}

- (void) changeCity

{

JRViewController * jrVC=[[JRViewController
alloc]init];

[self.navigationController
pushViewController:jrVC animated:YES];

}

@end

JSON文件

{

"weatherinfo": {

"city": "烟台",

"city_en": "yantai",

"date_y": "2015年6月15日",

"date": "",

"week": "星期一",

"fchh": "11",

"cityid": "101120501",

"temp1": "29℃~19℃",

"temp2": "31℃~19℃",

"temp3": "31℃~19℃",

"temp4": "28℃~18℃",

"temp5": "26℃~17℃",

"temp6": "26℃~17℃",

"tempF1": "84.2℉~66.2℉",

"tempF2": "87.8℉~66.2℉",

"tempF3": "87.8℉~66.2℉",

"tempF4": "82.4℉~64.4℉",

"tempF5": "78.8℉~62.6℉",

"tempF6": "78.8℉~62.6℉",

"weather1": "晴",

"weather2": "晴转多云",

"weather3": "多云",

"weather4": "多云转雷阵雨",

"weather5": "多云",

"weather6": "多云",

"img1": "0",

"img2": "99",

"img3": "0",

"img4": "1",

"img5": "1",

"img6": "99",

"img7": "1",

"img8": "4",

"img9": "1",

"img10": "99",

"img11": "1",

"img12": "99",

"img_single": "0",

"img_title1": "晴",

"img_title2": "晴",

"img_title3": "晴",

"img_title4": "多云",

"img_title5": "多云",

"img_title6": "多云",

"img_title7": "多云",

"img_title8": "雷阵雨",

"img_title9": "多云",

"img_title10": "多云",

"img_title11": "多云",

"img_title12": "多云",

"img_title_single": "晴",

"wind1": "南风3-4级",

"wind2": "南风3-4级",

"wind3": "南风3-4级",

"wind4": "东南风4-5级",

"wind5": "东南风4-5级转3-4级",

"wind6": "东北风转东风3-4级",

"fx1": "南风",

"fx2": "南风",

"fl1": "3-4级",

"fl2": "3-4级",

"fl3": "3-4级",

"fl4": "4-5级",

"fl5": "4-5级转3-4级",

"fl6": "3-4级",

"index": "热",

"index_d": "天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。",

"index48": "",

"index48_d": "",

"index_uv": "强",

"index48_uv": "",

"index_xc": "较适宜",

"index_tr": "适宜",

"index_co": "较舒适",

"st1": "28",

"st2": "18",

"st3": "30",

"st4": "17",

"st5": "30",

"st6": "17",

"index_cl": "较适宜",

"index_ls": "极适宜",

"index_ag": "不易发"

}

}

PS:该案例的关键之处在于截取JSON文件,一层一层解开,直到挑出对自己有用的部分~   (说的简单,解了一上午,把自己给绕进去了,蠢哭~~~)

时间: 2025-01-02 15:21:12

天气预报---网络加载最新天气信息,截取JSON文件的相关文章

Android异步加载学习笔记之四:利用缓存优化网络加载图片及ListView加载优化

如果不做任何处理,直接用网络加载图片在网速快的情况下可能没什么不好的感觉,但是如果使用移动流量或是网络不好的时候,问题就来了,要么用户会抱怨流量使用太多,要么抱怨图片加载太慢,如论从哪个角度出发,都不是好的体验!要提高用户体验,我们就要使用缓存.Android中数据缓存的方式有很多,相关介绍的文章也比较多,比如http://blog.csdn.net/dahuaishu2010_/article/details/17093139和http://www.jb51.net/article/38162

android快捷开发之Retrofit网络加载框架的简单使用

大家都知道,安卓最大的特点就是开源化,这自然会产生很多十分好用的第三方API,而基本每一个APP都会与网络操作和缓存处理机制打交道,当然,你可以自己通过HttpUrlConnection再通过返回数据进行解析解决,而我们自己学的东西大多数情况下都没有针对网络很糟糕的情况进行优化.下面就给大家带来Square Inc这家有创新精神的公司留下的Retrofit网络加载库的使用! Retrofit封装了从Web API下载数据,解析成一个普通的java对象(POJO),这里我们就去天狗网使用他们的一个

网络加载数据及单例工具类的代码抽取

今天给大家分享一一下网络加载数据:先看代码吧-------------------------- - (void)diseaseNumWithFinishedBlock:(FinishedBlocks)finishedBlock{ NSString *urlStr = @"http://数据接口"; NSDictionary *prama = @{响应参数}; [[NetWorkTool sharedNetWorkTool] POST:urlStr parameters:prama p

三种基本网络加载图片方式

代码片段(6) [全屏查看所有代码] 1. [代码]普通加载网络方式     ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 public class NormalLoadPictrue {   

异步网络加载开源框架AsyncHttpClient使用

AsyncHttpClient是异步的,但是有时候我们需要得到请求的结果集来返回给某个函数,由于是异步的,所以不能够直接return会去,所以可以定义一个interface来给调用AsyncHttpClient方法的一个回调来获取结果集,代码如下:定义回调接口: 点击(此处)折叠或打开 private static Callback mCallback; public interface Callback{ abstract void rankingsResultData(JSONArray r

【iOS】网络加载图片缓存与SDWebImage

加载网络图片可以说是网络应用中必备的.如果单纯的去下载图片,而不去做多线程.缓存等技术去优化,加载图片时的效果与用户体验就会很差. 一.自己实现加载图片的方法 tips: *iOS中所有网络访问都是异步的.(自己开线程去下载) *普通为模型增加UIImage属性的方法做的是内存缓存(下次启动还需要从网络重新加载), 而要做本地缓存的话,还要自己手动存储网络上下载的图片. *为了加快访问, 还需要自己去弄缓存.(内存缓存或者本地缓存) *当图片没有下载完成时,还要设置占位图片. 以下代码用NSOp

登录圆形头像之网络加载与缓存到本地

Android开发中常常有用户头像显示,似乎大多数都是圆形显示,如果每次加载网络头像,会频繁的请求网络,所以本文主要说的是登录时的头像网络加载和缓存到本地,以便于下次加载时直接从本地获取即可. 效果图 自定义控件实现圆形头像显示请看, Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示 这篇博客即可. 代码: public class MainActivity extends Activity implements OnClickListener { private Ima

Naigos PNP图无法加载最新数据

Naigos PNP图无法加载最新数据 前几天IDC断电,设备全部重启,导致nagios pnp图无法正常加载,经过排查发下如下几点状况: 1,查看log日志,并没有报错. 2,npcd没有正常运行,kill -9 后 /etc/ini 3,查看/usr/local/pnp4nagios/var/perfdata 下文件更新时间 无异常后,将文件赋予777权限 chmod 777 perfdata 4,查看iptables ,并没有违规策略与其冲突. 5,重启各个服务,nagios主进程,htt

android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存

经常会用到 网络文件 比如查看大图片数据 资源优化的问题,当然用开源的项目  Android-Universal-Image-Loader  或者 ignition 都是个很好的选择. 在这里把原来 写过的优化的代码直接拿出来,经过测试千张图片效果还是不错的. 免费培训课:http://www.jinhusns.com/Products/Curriculum/?type=xcj 工程目录 至于 Activity 就是加载了 1个网格布局 01./** 02.*   实现 异步加载 和   2级缓