声明:由于ASIHttpRequest作者已早不进行维护,而且ios版本已经更新到7,为了大家的代码在ios7下能更加高效和保险,建议使用AFNetworking类库,此类库也是ASI的作者所推荐的,如只是学习了解网络方面内容,可以参考本博客,关于AFNetworking的使用,相关博客本人会尽快整理
一、下载ASIHttpRequest的类库和JSONKit类库,添加到工程中
二、添加framework(参考链接中第十二步)
CFNetwork.framework SystemConfiguration.framework MobileCoreServices.framework CoreGraphics.framework libz.1.2.3.dylib
三、网络请求
声明URL:此处以镇江为例,更多城市代码
在.m文件中
#import"ASIHttpRequest.h"
#import"JSONKit.h"
#define GET_WEATHER_URL [NSURL URLWithString:@"http://m.weather.com.cn/data/101190301.html"]
在.h文件中声明ASIHttpRequestDelegate
<ASIHTTPRequestDelegate>在需要请求网络部分添加代码
//定义request ASIHTTPRequest *requestWeather = [ASIHTTPRequest requestWithURL:GET_WEATHER_URL]; //设置代理 requestWeather.delegate = self; //设置请求成功的回调方法,也可设置失败方法didFailSelector,如果调用失败方法,说明未成功,方便自己进行调试和处理 requestWeather.didFinishSelector = @selector(requestWeatherFinished:); //开始异步请求 [requestWeather startAsynchronous];
完成回调方法(其中的WeatherModal是自定义的一个类,为了方便存储返回的json数据,见下文)
-(void)requestWeatherFinished:(ASIHTTPRequest *)request { NSDictionary *object = [[request responseString] JSONValue];//获取返回数据,有时有些网址返回数据是NSArray类型,可先获取后打印出来查看数据结构,再选择处理方法,得到所需数据 //获取object中数据 NSDictionary *weather = [object objectForKey:@"weatherinfo"]; weatherModal *modal = [weatherModal alloc] init];//若未使用ARC,记得release modal.city = [weather objectForKey:@"city"]; //城市名称 modal.date_y = [weather objectForKey:@"date_y"];//日期 // 六天天气 modal.weather1 = [weather objectForKey:@"weather1"]; modal.weather2 = [weather objectForKey:@"weather2"]; modal.weather3 = [weather objectForKey:@"weather3"]; modal.weather4 = [weather objectForKey:@"weather4"]; modal.weather5 = [weather objectForKey:@"weather5"]; modal.weather6 = [weather objectForKey:@"weather6"]; // 六天风速 modal.wind1 = [weather objectForKey:@"wind1"]; modal.wind2 = [weather objectForKey:@"wind2"]; modal.wind3 = [weather objectForKey:@"wind3"]; modal.wind4 = [weather objectForKey:@"wind4"]; modal.wind5 = [weather objectForKey:@"wind5"]; modal.wind6 = [weather objectForKey:@"wind6"]; // 天气变化 modal.img_title1 = [weather objectForKey:@"img_title1"]; modal.img_title2 = [weather objectForKey:@"img_title2"]; modal.img_title3 = [weather objectForKey:@"img_title3"]; modal.img_title4 = [weather objectForKey:@"img_title4"]; modal.img_title5 = [weather objectForKey:@"img_title5"]; modal.img_title6 = [weather objectForKey:@"img_title6"]; modal.img_title7 = [weather objectForKey:@"img_title7"]; modal.img_title8 = [weather objectForKey:@"img_title8"]; modal.img_title9 = [weather objectForKey:@"img_title9"]; modal.img_title10 = [weather objectForKey:@"img_title10"]; modal.img_title11= [weather objectForKey:@"img_title11"]; modal.img_title12= [weather objectForKey:@"img_title12"]; // 温度 modal.temp1 = [weather objectForKey:@"temp1"]; modal.temp2 = [weather objectForKey:@"temp2"]; modal.temp3 = [weather objectForKey:@"temp3"]; modal.temp4 = [weather objectForKey:@"temp4"]; modal.temp5 = [weather objectForKey:@"temp5"]; modal.temp6 = [weather objectForKey:@"temp6"]; modal.week = [weather objectForKey:@"week"]; }
WeatherModal.h,此类是自定义类型,若需要像NSString一样拷贝赋值,只需加上一个方法,请参考自定义类拷贝办法
#import <Foundation/Foundation.h> @interface weatherModal : NSObject { NSString *_city; NSString *_city_en; NSString *_cityid; NSString *_date; NSString *_date_y; NSString *_img_single; NSString *_img_title1; NSString *_img_title10; NSString *_img_title11; NSString *_img_title12; NSString *_img_title2; NSString *_img_title3; NSString *_img_title4; NSString *_img_title5; NSString *_img_title6; NSString *_img_title7; NSString *_img_title8; NSString *_img_title9; NSString *_img_title_single; NSString *_temp1; NSString *_temp2; NSString *_temp3; NSString *_temp4; NSString *_temp5; NSString *_temp6; NSString *_weather1; NSString *_weather2; NSString *_weather3; NSString *_weather4; NSString *_weather5; NSString *_weather6; NSString *_week; NSString *_wind1; NSString *_wind2; NSString *_wind3; NSString *_wind4; NSString *_wind5; NSString *_wind6; } @property(nonatomic,copy) NSString *city; @property(nonatomic,copy) NSString *city_en; @property(nonatomic,copy) NSString *cityid; @property(nonatomic,copy) NSString *date; @property(nonatomic,copy) NSString *date_y; @property(nonatomic,copy) NSString *img_single; @property(nonatomic,copy) NSString *img_title1; @property(nonatomic,copy) NSString *img_title10; @property(nonatomic,copy) NSString *img_title11; @property(nonatomic,copy) NSString *img_title12; @property(nonatomic,copy) NSString *img_title2; @property(nonatomic,copy) NSString *img_title3; @property(nonatomic,copy) NSString *img_title4; @property(nonatomic,copy) NSString *img_title5; @property(nonatomic,copy) NSString *img_title6; @property(nonatomic,copy) NSString *img_title7; @property(nonatomic,copy) NSString *img_title8; @property(nonatomic,copy) NSString *img_title9; @property(nonatomic,copy) NSString *img_title_single; @property(nonatomic,copy) NSString *temp1; @property(nonatomic,copy) NSString *temp2; @property(nonatomic,copy) NSString *temp3; @property(nonatomic,copy) NSString *temp4; @property(nonatomic,copy) NSString *temp5; @property(nonatomic,copy) NSString *temp6; @property(nonatomic,copy) NSString *weather1; @property(nonatomic,copy) NSString *weather2; @property(nonatomic,copy) NSString *weather3; @property(nonatomic,copy) NSString *weather4; @property(nonatomic,copy) NSString *weather5; @property(nonatomic,copy) NSString *weather6; @property(nonatomic,copy) NSString *week; @property(nonatomic,copy) NSString *wind1; @property(nonatomic,copy) NSString *wind2; @property(nonatomic,copy) NSString *wind3; @property(nonatomic,copy) NSString *wind4; @property(nonatomic,copy) NSString *wind5; @property(nonatomic,copy) NSString *wind6; @end
weatherModal.m中对这些属性进行synthesize即可
PS:在解析json值时若是值关键字比较多均可定义一个modal进行存储数据,比较方便
附上我做的效果图,天气图片不是解析得到(也可解析得到,安卓成功,但本人ios没有解析成功,原因未知),是自己找好图片后,根据关键字进行匹配放置的
图片共享一下
晴天 多云 阵雨 阴天 小雨 中雨 大雨 大到暴雨 雷阵雨