swift2与AFNetworking3简单程序

网上关于AFNetworking的代码,基本上都是使用ObjectC语言。以下是swift2.0版本的获取天气信息的小程序,供初学者参考:

let manager = AFHTTPSessionManager()

let url = "http://v.juhe.cn/weather/index"

let params = ["format":2,"cityname":"深圳","key":"***"] //key被我隐藏了,可以去www.juhe.cn申请自己的key

manager.GET(url, parameters: params, progress: nil,success: { (task: NSURLSessionDataTask, responseObject: AnyObject?) -> Void in

print(responseObject)

}, failure: { (task: NSURLSessionDataTask?, error : NSError) -> Void in

print("请求失败")

})

在xcode中,我们可以看到返回的数据:

Optional({

"error_code" = 0;

reason = "successed!";

result =     {

future =         (

{

date = 20161230;

temperature = "13\U2103~17\U2103";

weather = "\U591a\U4e91";

"weather_id" =                 {

fa = 01;

fb = 01;

};

week = "\U661f\U671f\U4e94";

wind = "\U5fae\U98ce";

},

{

date = 20161231;

temperature = "16\U2103~22\U2103";

weather = "\U591a\U4e91";

"weather_id" =                 {

fa = 01;

fb = 01;

};

week = "\U661f\U671f\U516d";

wind = "\U5fae\U98ce";

},

{

date = 20170101;

temperature = "17\U2103~21\U2103";

weather = "\U591a\U4e91";

"weather_id" =                 {

fa = 01;

fb = 01;

};

week = "\U661f\U671f\U65e5";

wind = "\U5fae\U98ce";

},

{

date = 20170102;

temperature = "19\U2103~24\U2103";

weather = "\U591a\U4e91";

"weather_id" =                 {

fa = 01;

fb = 01;

};

week = "\U661f\U671f\U4e00";

wind = "\U5fae\U98ce";

},

{

date = 20170103;

temperature = "19\U2103~26\U2103";

weather = "\U591a\U4e91";

"weather_id" =                 {

fa = 01;

fb = 01;

};

week = "\U661f\U671f\U4e8c";

wind = "\U5fae\U98ce";

},

{

date = 20170104;

temperature = "16\U2103~22\U2103";

weather = "\U591a\U4e91";

"weather_id" =                 {

fa = 01;

fb = 01;

};

week = "\U661f\U671f\U4e09";

wind = "\U5fae\U98ce";

},

{

date = 20170105;

temperature = "17\U2103~21\U2103";

weather = "\U591a\U4e91";

"weather_id" =                 {

fa = 01;

fb = 01;

};

week = "\U661f\U671f\U56db";

wind = "\U5fae\U98ce";

}

);

sk =         {

humidity = "48%";

temp = 13;

time = "11:13";

"wind_direction" = "\U5317\U98ce";

"wind_strength" = "3\U7ea7";

};

today =         {

city = "\U6df1\U5733";

"comfort_index" = "";

"date_y" = "2016\U5e7412\U670830\U65e5";

"dressing_advice" = "\U5efa\U8bae\U7740\U539a\U5916\U5957\U52a0\U6bdb\U8863\U7b49\U670d\U88c5\U3002\U5e74\U8001\U4f53\U5f31\U8005\U5b9c\U7740\U5927\U8863\U3001\U5462\U5916\U5957\U52a0\U7f8a\U6bdb\U886b\U3002";

"dressing_index" = "\U8f83\U51b7";

"drying_index" = "";

"exercise_index" = "\U8f83\U9002\U5b9c";

temperature = "13\U2103~17\U2103";

"travel_index" = "\U8f83\U9002\U5b9c";

"uv_index" = "\U5f31";

"wash_index" = "\U8f83\U9002\U5b9c";

weather = "\U591a\U4e91";

"weather_id" =             {

fa = 01;

fb = 01;

};

week = "\U661f\U671f\U4e94";

wind = "\U5fae\U98ce";

};

};

resultcode = 200;

})

测试下,我们在浏览器中直接打开网址:http://v.juhe.cn/weather/index?key=***&cityname=深圳

返回的数据为:

{"resultcode":"200","reason":"successed!","result":{"sk":{"temp":"13","wind_direction":"北风","wind_strength":"3级","humidity":"48%","time":"11:04"},"today":{"temperature":"13℃~17℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"微风","week":"星期五","city":"深圳","date_y":"2016年12月30日","dressing_index":"较冷","dressing_advice":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","uv_index":"弱","comfort_index":"","wash_index":"较适宜","travel_index":"较适宜","exercise_index":"较适宜","drying_index":""},"future":{"day_20161230":{"temperature":"13℃~17℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"微风","week":"星期五","date":"20161230"},"day_20161231":{"temperature":"16℃~22℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"微风","week":"星期六","date":"20161231"},"day_20170101":{"temperature":"17℃~21℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"微风","week":"星期日","date":"20170101"},"day_20170102":{"temperature":"19℃~24℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"微风","week":"星期一","date":"20170102"},"day_20170103":{"temperature":"19℃~26℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"微风","week":"星期二","date":"20170103"},"day_20170104":{"temperature":"17℃~21℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"微风","week":"星期三","date":"20170104"},"day_20170105":{"temperature":"17℃~21℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"微风","week":"星期四","date":"20170105"}}},"error_code":0}

关于在swift中下载和配置AFNetworking,初学者可以参考下面链接里的文章:

http://www.cnblogs.com/GarveyCalvin/p/4123642.html

里面的示例,使用的是旧版的AFNetworking。如果你使用的是AFNetworking3,请忽略里面的示例,直接使用我上面的例子。

你可能感兴趣的:(swift2与AFNetworking3简单程序)