swift 同步 网络请求_Swift3.0:Get/Post同步和异步请求

//

//GetPostViewController.swift//NetWorkTest//

//Created by 夏远全 on 2017/4/3.//Copyright © 2017年 夏远全. All rights reserved.//import UIKit

class GetPostViewController: UIViewController {//在控制器定义全局的可变data,用户存储接收的数据

var jsonData:NSMutableData =NSMutableData()

override func viewDidLoad() {

super.viewDidLoad()

}//========================================Get==========================================//

//MARK: - 同步Get方式

func synchronousGet(){//1、创建URL对象;

let url:URL! = URL(string:"http://api.3g.ifeng.com/clientShortNews?type=beauty");//2、创建Request对象

//url: 请求路径

//cachePolicy: 缓存协议

//timeoutInterval: 网络请求超时时间(单位:秒)

let urlRequest:URLRequest = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)//3、响应对象

var response:URLResponse?

//4、发出请求

do{

let received= try NSURLConnection.sendSynchronousRequest(urlRequest, returning: &response)

let dic= try JSONSerialization.jsonObject(with: received, options: JSONSerialization.ReadingOptions.allowFragments)

print(dic)//let jsonStr = String(data: received, encoding:String.Encoding.utf8);

//print(jsonStr)

}catchlet error{

print(error.localizedDescription);

}

}//MARK: - 异步Get方式

func asynchronousGet(){//1、创建URL对象;

let url:URL! = URL(string:"http://api.3g.ifeng.com/clientShortNews?type=beauty");//2、创建Request对象

//url: 请求路径

//cachePolicy: 缓存协议

//timeoutInterval: 网络请求超时时间(单位:秒)

let urlRequest:URLRequest = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)//3、连接服务器

let connection:NSURLConnection? =NSURLConnection(request: urlRequest, delegate: self)

connection?.schedule(in: .current, forMode: .defaultRunLoopMode)

connection?.start()

}//========================================Post==========================================//

//MARK: - 同步Post方式

func synchronousPost() {//1、创建URL对象;

let url:URL! = URL(string:"http://api.3g.ifeng.com/clientShortNews");//2、创建Request对象

//url: 请求路径

//cachePolicy: 缓存协议

//timeoutInterval: 网络请求超时时间(单位:秒)

var urlRequest:URLRequest = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)//3、设置请求方式为POST,默认是GET

urlRequest.httpMethod = "POST"

//4、设置参数

let str:String = "type=beauty"let data:Data= str.data(using: .utf8, allowLossyConversion: true)!urlRequest.httpBody=data;//5、响应对象

var response:URLResponse?

//6、发出请求

do{

let received= try NSURLConnection.sendSynchronousRequest(urlRequest, returning: &response)

let dic= try JSONSerialization.jsonObject(with: received, options: JSONSerialization.ReadingOptions.allowFragments)

print(dic)//let jsonStr = String(data: received, encoding:String.Encoding.utf8);

//print(jsonStr)

}catchlet error{

print(error.localizedDescription);

}

}//MARK: - 异步Post方式

func asynchronousPost(){//1、创建URL对象;

let url:URL! = URL(string:"http://api.3g.ifeng.com/clientShortNews");//2、创建Request对象

//url: 请求路径

//cachePolicy: 缓存协议

//timeoutInterval: 网络请求超时时间(单位:秒)

var urlRequest:URLRequest = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)//3、设置请求方式为POST,默认是GET

urlRequest.httpMethod = "POST"

//4、设置参数

let str:String = "type=beauty"let data:Data= str.data(using: .utf8, allowLossyConversion: true)!urlRequest.httpBody=data;//3、连接服务器

let connection:NSURLConnection? =NSURLConnection(request: urlRequest, delegate: self)

connection?.schedule(in: .current, forMode: .defaultRunLoopMode)

connection?.start()

}

}//MARK - NSURLConnectionDataDelegate

extension GetPostViewController:NSURLConnectionDataDelegate{

func connection(_ connection: NSURLConnection, didReceive response: URLResponse) {//接收响应

}

func connection(_ connection: NSURLConnection, didReceive data: Data) {//收到数据

self.jsonData.append(data);

}

func connectionDidFinishLoading(_ connection: NSURLConnection) {//请求结束

//let jsonStr = String(data: self.jsonData as Data, encoding:String.Encoding.utf8);

//print(jsonStr)

do{

let dic= try JSONSerialization.jsonObject(with: self.jsonData as Data, options: JSONSerialization.ReadingOptions.allowFragments)

print(dic)

}catchlet error{

print(error.localizedDescription);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值