菜单

熟稔iOS网络编制程序之Http NSUEscortLSession(下)

2019年9月14日 - 皇家赌场系统

澳门皇家赌场 1革码者集散地

简单:NSU福睿斯LSession的采用特别轻便,先依照会话对象创设贰个伸手Task,然后施行该Task就能够。

抽象:NSU瑞虎LSessionTask本人是多个抽象类,在运用的时候,常常是依附现实的要求使用它的几个子类。关系如下:

澳门皇家赌场 2革码者营地

一、发送GET请求 使用NSU中华VLSession发送GET乞求的措施漫天进程如下:
1)鲜明乞求路线,GET须要参数直接跟在U昂科雷L后边2)创制诉求对象(暗许包括了须求头和央求方法,可计划3)成立会话对象(NSUWranglerLSession),可配置
4)依照会话对象创建诉求职分(NSUENCORELSessionDataTask) 5)实践Task
6)当获得服务器重返的响应后,深入分析数据(XML|JSON|HTTP)

//普通的HTTP get请求 func getRequest(){ // 获取Url --- 这个是我获取的天气预报接口 let url:NSURL = NSURL(string: "http://aqicn.org/publishingdata/json")! // 转换为requset let requets:NSURLRequest = NSURLRequest //NSURLSession 对象都由一个 NSURLSessionConfiguration 对象来进行初始化,后者指定了刚才提到的那些策略以及一些用来增强移动设备上性能的新选项 let configuration:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration() //backgroundSessionConfigurationWithIdentifier 后台处理配置 //ephemeralSessionConfiguration 这个是基于RAM的方式处理配置,数据时临时性的 //defaultSessionConfiguration 这个是默认配置 let session:NSURLSession = NSURLSession(configuration: configuration) //NSURLSessionTask负责处理数据的加载以及文件和数据在客户端与服务端之间的上传和下载,NSURLSessionTask 与 NSURLConnection 最大的相似之处在于它也负责数据的加载,最大的不同之处在于所有的 task 共享其创造者 NSURLSession 这一公共委托者(common delegate) let task:NSURLSessionDataTask = session.dataTaskWithRequest(requets, completionHandler: { (data:NSData?,response:NSURLResponse?,error:NSError?)->Void in if error == nil{ do{ let responseData = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSArray print(responseData) dispatch_async(dispatch_get_main_queue -> Void in self.dataLabel.text = responseData.firstObject!["cityName"]! as? String }) }catch{ } } }) // 启动任务 task.resume() }

//带header的HTTP get 请求 @IBAction func getRequestWithHeader(sender: AnyObject) { let url:NSURL = NSURL(string: "http://apis.baidu.com/heweather/pro/weather?city=beijing")! let request:NSMutableURLRequest = NSMutableURLRequest request.HTTPMethod = "GET" // 请求的Header request.addValue("a566eb03378211f7dc9ff15ca78c2d93", forHTTPHeaderField: "apikey") let configuration:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration() let session:NSURLSession = NSURLSession(configuration: configuration) let task:NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: { (data:NSData?,response:NSURLResponse?,error:NSError?)->Void in if error == nil{ do{ let responseData:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary print("普通带头与参数的GET请求 --- > \(responseData)") }catch{ } } }) task.resume() }

//get请求实战应用:写入文件。 override func viewDidLoad() { super.viewDidLoad() //let config = NSURLSessionConfiguration.backgroundSessionConfiguration//不赞成使用 //let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier//委托中不能使用,使用系统处理下载,就算APP没有运行了,也可以实现 //let config = NSURLSessionConfiguration.ephemeralSessionConfiguration();//这个是临时数据下载,适用于小数据下载 let config = NSURLSessionConfiguration.defaultSessionConfiguration()//默认配置 config.timeoutIntervalForRequest = 15 //连接超时时间 session = NSURLSession(configuration: config, delegate: self, delegateQueue:nil)//队列中,如果想要程序在主线程中执行,可以使用NSOperationQueue.mainQueue() let url = NSURL(string: "http://www.jianshu.com/p/e89f4b40bd85") let task = session.dataTaskWithURL { (data, response, error) -> Void in if (error == nil){ let str = NSString(data: data!, encoding: NSUTF8StringEncoding) print self.session.finishTasksAndInvalidate() //确保执行完成后,释放session let manager = NSFileManager.defaultManager() do{ var destinationPath = try manager.URLForDirectory(.CachesDirectory, inDomain: .UserDomainMask, appropriateForURL: url, create: true) // 获取//符号后面的string let componenetsOfUrl = url?.absoluteString.componentsSeparatedByString let index = componenetsOfUrl!.count - 1 let fileNameFromUrl = componenetsOfUrl![index]+".text" destinationPath = destinationPath.URLByAppendingPathComponent(fileNameFromUrl) try str?.writeToURL(destinationPath, atomically: true, encoding: NSUTF8StringEncoding) let message = "保存下载数据到 = \(destinationPath)" self.displayAlertWithTitle("Success", message: message) print }catch{ print } }else{ self.displayAlertWithTitle("Error", message: "不能下载这数据,一个错误抛出") } } task.resume() } func displayAlertWithTitle(title:String,message:String){ let controller = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) controller.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) presentViewController(controller, animated: true, completion: nil) }

//get请求实战应用:后台下载处理import UIKitextension NSURLSessionTask{ func start(){ self.resume() }}class ViewController3:UIViewController,NSURLSessionDelegate,NSURLSessionDownloadDelegate,NSURLSessionTaskDelegate { var session:NSURLSession! var configidentifier:String{ let userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults() let key = "time" let times = userDefaults.stringForKey if let thetime = times { return thetime }else{ let newtime = NSDate().description userDefaults.setObject(newtime, forKey: key) userDefaults.synchronize() return newtime } } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! } override func viewDidAppear(animated: Bool) { super.viewDidAppear let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(self.configidentifier) config.timeoutIntervalForRequest = 15 session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil) let url = NSURL(string: "http://www.jianshu.com/p/e89f4b40bd85") let task = session.downloadTaskWithURL task.start() } func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) { print(downloadTask.response) let progress = totalBytesWritten / totalBytesExpectedToWrite print(bytesWritten) print(totalBytesWritten) print(totalBytesExpectedToWrite)//如果服务器未返回总长度,这里就显示-1 print NSLog } func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) { do{ // 将临时文件剪切或者复制其他文件夹才能看到 let fileManager = NSFileManager.defaultManager() let cachePath = try fileManager.URLForDirectory(.CachesDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true) let saveFileName = (cachePath.path)! + "/" + (downloadTask.response?.suggestedFilename)! try fileManager.moveItemAtPath(location.path!, toPath: saveFileName) NSLog }catch{ } } func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) { NSLog session.finishTasksAndInvalidate() }

二、发送POST请求澳门皇家赌场, 使用NSUTiggoLSession发送POST央求的方法漫天进度如下:
1)明确诉求路线(一般由店家的后台开采职员以接口文书档案的办法提供)
2)创制可变的伸手对象,此步骤不得以轻易 3)修改诉求方法为POST
4)设置央求体,把参数调换为二进制数据并设置要求体
5)创设会话对象(NSUMuranoLSession)
6)依照会话对象创设诉求职责(NSU福特ExplorerLSessionDataTask) 7)试行Task
8)当获得服务器重临的响应后,剖析数据(XML|JSON|HTTP)

//普通的HTTP Post请求 func postRequest(){ let request = NSMutableURLRequest(URL: NSURL(string: "http://120.25.226.186:32812/login")!) // 这块就是区别啦,其实也差不多 request.HTTPMethod = "POST" let postString = "username=520it&pwd=520it&type=JSON" request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data:NSData?,response:NSURLResponse?,error:NSError?)->Void in if error == nil{ do{//解析方式1 let responseString = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary print("Post --- > \(responseString)") let result = responseString["success"]?.dataUsingEncoding(NSUTF8StringEncoding) let result1 = NSString(data: result!, encoding:NSUTF8StringEncoding ) print// 解析方式2 let responseString2 = NSString(data: data!, encoding: NSUTF8StringEncoding) print("Post --- > \(responseString2)") }catch{ print("have catch") } } }) task.resume() }

//post请求实战应用:上传class ViewController4:UIViewController,NSURLSessionDelegate,NSURLSessionDataDelegate{ var session:NSURLSession! func displayAlertWithTitle(title:String,message:String){ let controller:UIAlertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) controller.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) presentViewController(controller, animated: true, completion: nil) } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! } override func viewDidAppear(animated: Bool) { super.viewDidAppear let config = NSURLSessionConfiguration.defaultSessionConfiguration() config.timeoutIntervalForRequest = 15 session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil) let datatoup = "Hello World".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) let url = NSURL(string: "http://www.jianshu.com/p/e89f4b40bd85") let request = NSMutableURLRequest(URL: url!) request.HTTPMethod = "POST" let task = session.uploadTaskWithRequest(request, fromData: datatoup!) task.start() } func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) { session.finishTasksAndInvalidate() NSLog("错误 = \ dispatch_async(dispatch_get_main_queue(), {[weak self] () -> Void in var message = "完成上传数据" if error != nil { message = "上传内容失败" } self?.displayAlertWithTitle("信息", message: message) }) } }

迄今,小说已经记下了HTTP中的GET和POST央求的骨干使用。希望对上学互连网编制程序的码儿有所补助。

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图