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,请忽略里面的示例,直接使用我上面的例子。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • JSON数据格式: 数组和字典的关系 -好基友,套来套去 字典套 数组套 字典 { "succes...
    whong736阅读 1,778评论 0 0
  • How to receive a million packets per second16 Jun 2015 by...
    小学生auxten阅读 8,141评论 0 5
  • 海边,一位身着灯笼裤,头戴着花环,脸颊顶着两块高原红的少女,用她那独特的台湾腔,缓缓的讲解着动作要领,并将自己的身...
    梦婷子阅读 3,392评论 0 0
  • 1.假设对方是个“小白” 如何更加简单明了通俗易懂地把信息传达给对方,就是把对方当做啥都不懂的小白,通过把深奥难懂...
    咿呀作语阅读 1,238评论 0 1
  • 日本曾有一部电影《切腹》,影片中主人公的女婿被以武士道荣誉之名逼得切腹。但当主人公打倒这几个逼死他女婿的几...
    WookHou阅读 3,392评论 0 1

友情链接更多精彩内容