//
//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);
}
}
}