异步Post方式

      // MARK: - 异步Post方式
     func asynchronousPost()
     {
         //创建NSURL对象
         var url:NSURL! = NSURL(string: "http://m.weather.com.cn/data/101010100.html")

         //创建请求对象
         var request : NSMutableURLRequest = NSMutableURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: )

         request.HTTPMethod = "POST"//设置请求方式为POST,默认为GET

         var str:String = "type=focus-c";//设置参数
         var data:NSData = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!
         request.HTTPBody = data;

         //连接服务器
         var connection = NSURLConnection(request: request, delegate: self)
     }

     // MARK: - NSURLConnectionDataDelegate
     func connection(connection: NSURLConnection, willSendRequest request: NSURLRequest, redirectResponse response: NSURLResponse?) -> NSURLRequest?
     {
         //将要发送请求
         return request
     }

     func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse)
     {
         //接收响应
     }

     var jsonData:NSMutableData =  NSMutableData()
     func connection(connection: NSURLConnection, didReceiveData data: NSData)
     {
         //收到数据
         self.jsonData.appendData(data)
     }

     func connection(connection: NSURLConnection, needNewBodyStream request: NSURLRequest) -> NSInputStream?
     {
         //需要新的内容流
         return request.HTTPBodyStream
     }

     func connection(connection: NSURLConnection, didSendBodyData bytesWritten: Int, totalBytesWritten: Int, totalBytesExpectedToWrite: Int)
     {
         //发送数据请求
     }

     func connection(connection: NSURLConnection, willCacheResponse cachedResponse: NSCachedURLResponse) -> NSCachedURLResponse?
     {
         //缓存响应
         return cachedResponse
     }

     func connectionDidFinishLoading(connection: NSURLConnection)
     {
         //请求结束

         var jsonString = NSString(data: self.jsonData, encoding: NSUTF8StringEncoding)

         println(jsonString)

         let dict:AnyObject? = NSJSONSerialization.JSONObjectWithData(self.jsonData, options: NSJSONReadingOptions.AllowFragments, error: nil)

         //已下代码 重新修订
 //        var dic = dict as NSDictionary
 //
 //        let weatherinfoDic = dic.objectForKey("weatherinfo") as? NSDictionary
 //        let city = weatherinfoDic?.objectForKey("city") as? String
 //        let date_y = weatherinfoDic?.objectForKey("date_y") as? String
 //        let temp1 = weatherinfoDic?.objectForKey("temp1") as? String
 //
 //        let alert = UIAlertView(title: (city! + date_y!), message: temp1!, delegate: nil, cancelButtonTitle: "确定")
 //        alert.show()

         //2015年4月10号修改
         if var dic = dict as? NSDictionary
         {
             let weatherinfoDic = dic.objectForKey("weatherinfo") as? NSDictionary
             let city = weatherinfoDic?.objectForKey("city") as? String
             let date_y = weatherinfoDic?.objectForKey("date_y") as? String
             let temp1 = weatherinfoDic?.objectForKey("temp1") as? String

             let alert = UIAlertView(title: (city! + date_y!), message: temp1!, delegate: nil, cancelButtonTitle: "确定")
             alert.show()
         }
     }
     
 

最新文章

  1. SqlServer -- 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'T_FM_AMTFLOW'中的标识列指定显式值。
  2. c# 根据配置文件路径,设置和获取config文件 appSettings 节点值
  3. RequestContextListener作用
  4. Scala 深入浅出实战经典 第75讲:模式匹配下的For循环
  5. 实现 UISegmentControl 与 UIScrollView的上下级联(分别在相应的方法中加入级联代码)
  6. C-crash的方法
  7. canvas 绘制矩形和圆形
  8. Aspose.Cells 读取Excel数据到DataTable
  9. POJ3253Babelfish
  10. UVa 201 Squares
  11. Timeout for tests
  12. cocos2D(四)---- CCSprite
  13. iOS_11_tableViewCell使用alertView变更数据
  14. TemplateMethod-模板模式
  15. MUI 图片上传实现
  16. git添加本地仓库与远程仓库连接
  17. c#二进制移位运算符 "<<"及">>","&","|","^"
  18. 6个小而美的es6新特性
  19. Photo Sphere Viewer 全景图
  20. c#如何解析时区字符串

热门文章

  1. 完美完全卸载Oracle 11g数据库
  2. How to Calculate difference between two dates in C# z
  3. [转]Oracle 阳历转农历
  4. 《Python核心编程》 第七章 映射和集合类型 - 习题
  5. Android相关图书推荐
  6. bzoj 2229 [Zjoi2011]最小割(分治+最小割)
  7. android 拍照或者图库选择 压缩后 图片 上传
  8. 生成chm文档工具- Sandcastle -摘自网络
  9. Gym 100507G The Debut Album (滚动数组dp)
  10. STL学习系列三:Deque容器