微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift几种简单的Web数据通讯方式

        //同期・Web数据取得 -1 文本形式
        var strHtml = Nsstring(contentsOfURL: NSURL(string: "http://www.google.com")!,encoding: NSUTF8StringEncoding,error: nil)
        println(strHtml)
        
        //同期・Web数据取得 -2 二进制数据
        var data = NSData(contentsOfURL: NSURL(string: "http://www.google.com")!)
        var str = Nsstring(data: data!,encoding: NSUTF8StringEncoding)
        println(str)
        //非同期・Web取得数据 -1 阻塞型
        var response: NSURLResponse?
        var error: NSError?
        
        var syncData = NSURLConnection.sendSynchronousRequest(NSURLRequest(URL: NSURL(string: "http://www.google.com")!),returningResponse: &response,error: &error)
        if let temp = syncData {
            var syncStr = Nsstring(data: temp,encoding: NSUTF8StringEncoding)
            println(syncStr)
        }
        
        if let r = response {
            //取得Response
            println(r)
        }
        
        if let e = error {
            //有Error时的处理
            println(e)
        }
        
        
        //非同期・Web取得数据 -2 非阻塞型
        NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: NSURL(string: "http://localhost:8080/Iphoneserver/hello.jsp?name=123")!),queue: NSOperationQueue(),{ (response: NSURLResponse!,data:NSData!,error: NSError!) -> Void in
            
            if let e = error {
                println(e)
            }
            
            println(Nsstring(data: data,encoding: NSUTF8StringEncoding)!)
        });

        //非同期・Web取得数据 -2 非阻塞型 -GET方式
        NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: NSURL(string: "http://localhost:8080/Iphoneserver/hello.jsp?name=\(paramName.text)")!),completionHandler: { (response: NSURLResponse!,error: NSError!) -> Void in
            
            if let e = error {
                println(e)
            }
            println(self.paramName.text)
            var resultString = Nsstring(data: data,encoding: NSUTF8StringEncoding)!
            println(resultString)
            
            //向主线程发送消息(发的是函数指针吧)
            dispatch_sync(dispatch_get_main_queue(),{ () -> Void in
                
                //由于queue参数指定了新开始一个线程来执行, 在使用NSOperationQueue()创建的副线程中是无法改变主线程的控件内容,所以使用dispatch_sync将处理发回主线程
                //或者queue: NSOperationQueue.mainQueue() 指定使用主线程序,但是主线程过度使用的时候,主界面还是会感觉卡顿,要注意
                self.resultTxt.text = resultString;
            })
            
        });

        //非同期・Web取得数据 -2 非阻塞型 -POST方式
        
        //可变Request对象
        var req = NSMutableuRLRequest(URL: NSURL(string: "http://localhost:8080/Iphoneserver/hello.jsp")!)
        
        req.HTTPMethod = "POST"
        req.HTTPBody = Nsstring(string: "name=\(paramName.text)").dataUsingEncoding(NSUTF8StringEncoding)
        
        NSURLConnection.sendAsynchronousRequest(req,queue: NSOperationQueue.mainQueue()) { (resp,data,error) -> Void in
            
            if let e = error {
                println(e)
            }
        
            if let d = data {
                self.resultTxt.text = Nsstring(data: d,encoding: NSUTF8StringEncoding)
            }
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐