学习地址:https://github.com/potato512/SYSwiftLearning
效果图
在swift中使用NSURLSessionDataTask进行网络编程。
- // NSURL
- let url:NSURL = NSURL(string:"http://rapapi.org/mockjsdata/22598/userloginGet")!
- // NSURLRequest
- let request:NSURLRequest = NSURLRequest(URL:url)
- // NSURLSession
- let configuration:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
- let session:NSURLSession = NSURLSession(configuration: configuration)
- // NSURLSessionTask
- let task:NSURLSessionDataTask = session.dataTaskWithRequest(request,completionHandler: {
- (data:NSData?,response:NSURLResponse?,error:NSError?) -> Void in
- if error == nil
- {
- do {
- let result:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!,options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
- print(result)
- dispatch_async(dispatch_get_main_queue(),{
- () -> Void in
- let message:String = result.objectForKey("msg") as! String
- let alert = UIAlertView(title: nil,message: message,delegate: nil,cancelButtonTitle: "OK")
- alert.show()
- })
- } catch {
- }
- }
- })
- // 启动任务
- task.resume()
- // NSURL
- let url:NSURL = NSURL(string:"http://rapapi.org/mockjsdata/22598/userloginPostWithParams")!
- // NSURLRequest
- let request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
- request.HTTPMethod = "POST"
- // 参数
- // 方法1
- // let postString = "userName=devZhang&userPassword=devZhang"
- // request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
- // 方法2
- let postDic :NSDictionary = ["userName":"devZhang","userPassword":"devZhang"]
- do {
- let data:NSData = try NSJSONSerialization.dataWithJSONObject(postDic,options: NSJSONWritingOptions.PrettyPrinted)
- request.HTTPBody = data
- } catch {
- }
- // NSURLSession
- let configuration:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
- let session:NSURLSession = NSURLSession(configuration: configuration)
- // NSURLSessionTask
- let task:NSURLSessionDataTask = session.dataTaskWithRequest(request,cancelButtonTitle: "OK")
- alert.show()
- })
- } catch {
- }
- }
- })
- // 启动任务
- task.resume()
- // 请求路径
- let url: NSURL = NSURL(string:"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON")!
- // 请求对象
- // 请求对象内部默认已经包含了请求头和请求方法(GET)
- let request: NSURLRequest = NSURLRequest(URL: url)
- // 会话对象,并设置代理
- /*
- 第一个参数:会话对象的配置信息 defaultSessionConfiguration 表示默认配置
- 第二个参数:谁成为代理,此处为控制器本身即self,协议 NSURLSessionDelegate
- 第三个参数:队列,该队列决定代理方法在哪个线程中调用,可以传主队列|非主队列
- [NSOperationQueue mainQueue] 主队列: 代理方法在主线程中调用
- [[NSOperationQueue alloc]init] 非主队列: 代理方法在子线程中调用
- */
- let session: NSURLSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),delegate: self,delegateQueue: NSOperationQueue.mainQueue())
- // 根据会话对象创建一个Task(发送请求)
- let dataTask: NSURLSessionTask = session.dataTaskWithRequest(request)
- // 执行任务
- dataTask.resume()
- // 接收到服务器响应的时候调用该方法
- func URLSession(session: NSURLSession,dataTask: NSURLSessionDataTask,didReceiveResponse response: NSURLResponse,completionHandler: (NSURLSessionResponseDisposition) -> Void) {
- // 响应头信息,即response
- print("didReceiveResponse--%@",response)
- // 注意:需要使用completionHandler回调告诉系统应该如何处理服务器返回的数据
- // 默认是取消的
- /*
- NSURLSessionResponseCancel = 0,默认的处理方式,取消
- NSURLSessionResponseAllow = 1,接收服务器返回的数据
- NSURLSessionResponseBecomeDownload = 2,变成一个下载请求
- NSURLSessionResponseBecomeStream 变成一个流
- */
- completionHandler(NSURLSessionResponseDisposition.Allow)
- }
- // 当请求完成(成功|失败)的时候会调用该方法,如果请求失败,则error有值
- func URLSession(session: NSURLSession,task: NSURLSessionTask,didCompleteWithError error: NSError?) {
- if (error == nil)
- {
- //解析数据,JSON解析请参考http://www.cnblogs.com/wendingding/p/3815303.html
- var dict:NSDictionary? = nil
- do {
- dict = try NSJSONSerialization.JSONObjectWithData(self.data,options: NSJSONReadingOptions.init(rawValue: 0)) as? NSDictionary
- } catch {
- }
- print("%@",dict)
- }
- }