
在IOS中使用NSURLConnection实现http通信,NSURLConnection提供了异步和同步两种通信方式,同步请求会造成进程阻塞,通常我们使用异步的方式,不管同步还是异步,建立通信的基本步骤都是一样的:
1,创建NSURL
2,创建Request对象
3,创建NSURLConnection连接
第3步结束后就建立了一个http连接。
这里我们用一个开放的API做例子:
http://www.weather.com.cn/adat/sk/101010100.HTML
这是北京市的当前天气信息的Json,我们首先来写一个同步的网络连接来获取这个Json,新建一个工程,在页面上添加一个按钮,每次点击按钮就会输出Json的内容到控制台,控制器代码:
import UIKitclass VIEwController: UIVIEwController { @IBAction func showWeatherjson(sender: UIbutton) { //创建url var url:NSURL! = NSURL(string: "http://www.weather.com.cn/adat/sk/101010100.HTML") //创建请求对象 var urlRequest:NSURLRequest = NSURLRequest(URL: url) //创建响应对象 var response:NSURLResponse? //创建错误对象 var error:NSError? //发出请求 var data:NSData? = NSURLConnection.sendSynchronousRequest(urlRequest,returningResponse: &response,error: &error) if error != nil { println(error?.code) println(error?.description) } else { var JsonString = Nsstring(data: data!,enCoding: NSUTF8StringEnCoding) println(JsonString) } }} 运行结果如下:
下面来展示异步请求的代码:
import UIKitclass VIEwController: UIVIEwController,NSURLConnectionDataDelegate,NSURLConnectionDelegate { @IBAction func getWeatherjson(sender: UIbutton) { //创建NSURL对象 var url:NSURL! = NSURL(string: "http://www.weather.com.cn/adat/sk/101010100.HTML") //创建请求对象 var urlRequest:NSURLRequest = NSURLRequest(URL: url) //网络连接对象 var conn:NSURLConnection? = NSURLConnection(request: urlRequest,delegate: self) conn?.scheduleInRunLoop(NSRunLoop.currentRunLoop(),forMode: NSRunLoopCommonModes) //执行 conn?.start() }} 然后在代理方法中添加代码即可,代理NSURLConnectionDataDelegate的代理方法如下:
func connection(connection: NSURLConnection,willSendRequest request: NSURLRequest,redirectResponse response: NSURLResponse?) -> NSURLRequest? { //将要发送请求 return request } func connection(connection: NSURLConnection,dIDReceiveResponse response: NSURLResponse) { //接收响应 } func connection(connection: NSURLConnection,dIDReceiveData data: NSData) { //收到数据 } func connection(connection: NSURLConnection,neednewBodyStream request: NSURLRequest) -> NSinputStream? { //需要新的内容流 return request.httpBodyStream } func connection(connection: NSURLConnection,dIDSendBodyData bytesWritten: Int,totalBytesWritten: Int,totalBytesExpectedToWrite: Int) { //发送数据请求 } func connection(connection: NSURLConnection,willCacheResponse cachedResponse: NSCachedURLResponse) -> NSCachedURLResponse? { //缓存响应 return cachedResponse } func connectionDIDFinishLoading(connection: NSURLConnection) { //请求结束 } 定义一个NSMutableData类型数据流,在dIDReceiveData代理方法中收集数据流,代码如下:
var JsonData:NSMutableData = NSMutableData() func connection(connection: NSURLConnection,dIDReceiveData data: NSData) { //收到数据 JsonData.appendData(data) } 在connectionDIDFinishLoading结束请求的代理方法内,解析JsonData数据流。代码如下:
func connectionDIDFinishLoading(connection: NSURLConnection) { //请求结束 var JsonString = Nsstring(data: JsonData,enCoding: NSUTF8StringEnCoding) println(JsonString) } 运行,同样得到结果:
总结
以上是内存溢出为你收集整理的swift语言IOS8开发战记24 解析Json全部内容,希望文章能够帮你解决swift语言IOS8开发战记24 解析Json所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)