码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——网络Swift篇&NSURL进行数据请求(POST与GET)

时间:2015-07-23 21:45:38      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

NSURL进行数据请求(POST与GET)

使用Swift进行iOS开发时,不可避免的要进行远程的数据获取和提交。
其数据请求的方式既可能是POST也可能是GET。同不管是POST还是GET又可以分为同步请求和异步请求。
下面通过四个例子来进行演示。
 
1,使用POST方式提交数据(用户id和分数)
(1)同步请求
 1 //保存分数
 2 func saveScore(score:Int, userid:String)
 3 {
 4     let urlString:String = "http://hangge.com/"
 5     var url:NSURL!
 6     url = NSURL(string:urlString)
 7     var request = NSMutableURLRequest(URL:url)
 8     var body = "score=\(score)&user=\(userid)"
 9     //编码POST数据
10     var postData = body.dataUsingEncoding(NSUTF8StringEncoding)
11     //保用 POST 提交
12     request.HTTPMethod = "POST"
13     request.HTTPBody = postData
14      
15     //响应对象
16     var response:NSURLResponse?
17     //错误对象
18     var error:NSError?
19      
20     //发出请求
21     var received:NSData? = NSURLConnection.sendSynchronousRequest(request,
22         returningResponse: &response, error: &error)
23      
24     if(error != nil){
25         println(error?.code)
26         println(error?.description)
27     }else{
28         var datastring = NSString(data:received!, encoding: NSUTF8StringEncoding)
29         println(datastring)
30     }
31 }

 


(2)异步请求
 1 import UIKit
 2  
 3 class ScoreController:NSObject, NSURLConnectionDataDelegate
 4 {
 5     //保存分数
 6     func saveScore(score:Int, userid:String)
 7     {
 8         let urlString:String = "http://hangge.com/savescore.php"
 9         var url:NSURL!
10         url = NSURL(string:urlString)
11         var request = NSMutableURLRequest(URL:url)
12         var body = "score=\(score)&user=\(userid)"
13         //编码POST数据
14         var postData = body.dataUsingEncoding(NSASCIIStringEncoding)
15         //保用 POST 提交
16         request.HTTPMethod = "POST"
17         request.HTTPBody = postData
18         var conn:NSURLConnection!
19         conn = NSURLConnection(request: request,delegate: self)
20         conn.start()
21         println(conn)
22     }
23         
24     func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!)
25     {
26         println("请求成功!");
27         println(response)
28     }
29      
30     func connection(connection: NSURLConnection!, didReceiveData data: NSData!)
31     {
32         println("请求成功1!");
33         var datastring = NSString(data:data, encoding: NSUTF8StringEncoding)
34         println(datastring)
35     }
36      
37     func connectionDidFinishLoading(connection: NSURLConnection!)
38     {
39         println("请求成功2!");
40     }
41 }

 


2,使用GET方式获取数据(用户id对应的分数)
(1)同步请求
 1 //获取分数
 2 func getScore(user:String){
 3      
 4     let urlString:String="http://hangge.com/getscore.php?user=\(user)"
 5     var url:NSURL!
 6     url = NSURL(string:urlString)
 7     var request = NSMutableURLRequest(URL:url)
 8     request.HTTPMethod = "GET"
 9      
10     //响应对象
11     var response:NSURLResponse?
12     //错误对象
13     var error:NSError?
14      
15     //发出请求
16     var received:NSData? = NSURLConnection.sendSynchronousRequest(request,
17         returningResponse: &response, error: &error)
18      
19     if(error != nil){
20         println(error?.code)
21         println(error?.description)
22     }else{
23         var datastring = NSString(data:received!, encoding: NSUTF8StringEncoding)
24         println(datastring)
25     }
26 }

 


(2)异步请求
 1 import UIKit
 2  
 3 class ScoreController:NSObject, NSURLConnectionDataDelegate
 4 {    
 5     //获取分数
 6     func getScore(user:String)
 7     {
 8         let urlString:String="http://hangge.com/getscore.php?user=\(user)"
 9         var url:NSURL!
10         url = NSURL(string:urlString)
11         var request = NSMutableURLRequest(URL:url)
12          
13         var body = "user=\(user)"
14         request.HTTPMethod = "GET"
15         var conn:NSURLConnection!
16         conn = NSURLConnection(request: request,delegate: self)
17         conn.start()
18         println(conn)
19     }
20      
21     func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!)
22     {
23         println("请求成功!");
24         println(response)
25     }
26      
27     func connection(connection: NSURLConnection!, didReceiveData data: NSData!)
28     {
29         println("请求成功1!");
30         var datastring = NSString(data:data, encoding: NSUTF8StringEncoding)
31         println(datastring)
32         //解析 JSON 数据
33         var json : AnyObject! = NSJSONSerialization.JSONObjectWithData(data,
34             options:NSJSONReadingOptions.AllowFragments,error:nil)
35          
36         var score = json.objectForKey("score") as Int
37         println(score)
38     }
39      
40     func connectionDidFinishLoading(connection: NSURLConnection!)
41     {
42         println("请求成功2!");
43     }
44 }

 

 

iOS开发——网络Swift篇&NSURL进行数据请求(POST与GET)

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4671546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!