异步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() } }