iOS开发——网络编程Swift篇&(六)异步Post方式
异步Post方式
1 // MARK: - 异步Post方式 2 func asynchronousPost() 3 { 4 //创建NSURL对象 5 var url:NSURL! = NSURL(string: "http://m.weather.com.cn/data/101010100.html") 6 7 //创建请求对象 8 var request : NSMutableURLRequest = NSMutableURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 10) 9 10 request.HTTPMethod = "POST"//设置请求方式为POST,默认为GET 11 12 var str:String = "type=focus-c";//设置参数 13 var data:NSData = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)! 14 request.HTTPBody = data; 15 16 //连接服务器 17 var connection = NSURLConnection(request: request, delegate: self) 18 } 19 20 21 22 // MARK: - NSURLConnectionDataDelegate 23 func connection(connection: NSURLConnection, willSendRequest request: NSURLRequest, redirectResponse response: NSURLResponse?) -> NSURLRequest? 24 { 25 //将要发送请求 26 return request 27 } 28 29 func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) 30 { 31 //接收响应 32 } 33 34 35 var jsonData:NSMutableData = NSMutableData() 36 func connection(connection: NSURLConnection, didReceiveData data: NSData) 37 { 38 //收到数据 39 self.jsonData.appendData(data) 40 } 41 42 func connection(connection: NSURLConnection, needNewBodyStream request: NSURLRequest) -> NSInputStream? 43 { 44 //需要新的内容流 45 return request.HTTPBodyStream 46 } 47 48 func connection(connection: NSURLConnection, didSendBodyData bytesWritten: Int, totalBytesWritten: Int, totalBytesExpectedToWrite: Int) 49 { 50 //发送数据请求 51 } 52 53 func connection(connection: NSURLConnection, willCacheResponse cachedResponse: NSCachedURLResponse) -> NSCachedURLResponse? 54 { 55 //缓存响应 56 return cachedResponse 57 } 58 59 func connectionDidFinishLoading(connection: NSURLConnection) 60 { 61 //请求结束 62 63 var jsonString = NSString(data: self.jsonData, encoding: NSUTF8StringEncoding) 64 65 println(jsonString) 66 67 68 let dict:AnyObject? = NSJSONSerialization.JSONObjectWithData(self.jsonData, options: NSJSONReadingOptions.AllowFragments, error: nil) 69 70 71 //已下代码 重新修订 72 // var dic = dict as NSDictionary 73 // 74 // let weatherinfoDic = dic.objectForKey("weatherinfo") as? NSDictionary 75 // let city = weatherinfoDic?.objectForKey("city") as? String 76 // let date_y = weatherinfoDic?.objectForKey("date_y") as? String 77 // let temp1 = weatherinfoDic?.objectForKey("temp1") as? String 78 // 79 // let alert = UIAlertView(title: (city! + date_y!), message: temp1!, delegate: nil, cancelButtonTitle: "确定") 80 // alert.show() 81 82 83 //2015年4月10号修改 84 if var dic = dict as? NSDictionary 85 { 86 let weatherinfoDic = dic.objectForKey("weatherinfo") as? NSDictionary 87 let city = weatherinfoDic?.objectForKey("city") as? String 88 let date_y = weatherinfoDic?.objectForKey("date_y") as? String 89 let temp1 = weatherinfoDic?.objectForKey("temp1") as? String 90 91 let alert = UIAlertView(title: (city! + date_y!), message: temp1!, delegate: nil, cancelButtonTitle: "确定") 92 alert.show() 93 } 94 } 95
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。