//MARK: - 同步Post方式
fileprivate 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)
} catch let error{
print(error.localizedDescription);
}
}
NetworkTool.requsetData("http://192.168.1.23/index.php?m=live&c=json_post&a=category", type: .post, parameters: ["num" : 10,"page":1]) { ( result: Any) in
var ChanarrM = [ChannelModel]()
// 将 Any 类型转换成字典类型
guard let resultDict = result as? [String : Any] else { return }
// 根据 hotWordList 的 key 取出内容
guard let dataArray = resultDict["data"] as? [[String : Any]] else { return }
// 遍历字典, 将字典转换成模型对象
for dict in dataArray {
let ab = ChannelModel(dict: dict as [String : AnyObject])
ChanarrM.append(ab)
}
//对模型中的tid进行排序
ChanarrM.sort(by: { (s1, s2) -> Bool in
//return s1.tid! < s2.tid!
//return s1.topicid! < s2.topicid!
return s1.catid! < s2.catid!
})
print(ChanarrM)
}