POST请求
funcPOST(url:String,pass:@escaping(Any,Bool)->Void) {
let urlStr = URL.init(string: "http://api.jisuapi.com/illegaladdr/city")
varreq =URLRequest.init(url: urlStr!, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval:10)
// 设置请求方式为POST
req.httpMethod="POST"
// 将所有的参数拼接成一个字符串
let str = "city=\(url)&num=10&appkey=de394933e1a3e2db"
req.httpBody= str.data(using: .utf8)
URLSession.shared.dataTask(with: req) { (data, response:URLResponse?, error)in
letjsonData =try?JSONSerialization.jsonObject(with: data!, options: .allowFragments)
letjsonDic = jsonDataas!NSDictionary
letstatus = jsonDic.value(forKey:"status")as!NSString
letmsg = jsonDic.value(forKey:"msg")as!String
ifstatus.intValue!=0{
print(msg)
return
}
// 得到json数据中result字段对应的字典
letresultDic = jsonDic.value(forKey:"result")as!NSArray
// // 得到result字典中list数组
// let listArr = resultDic.value(forKey: "list") as! NSArray
// Model封装
varmodelArr:[gfd] = []
foriteminresultDic {
letitemDic = itemas!NSDictionary
letone =gfd()
one.province= (itemDic.value(forKey:"province")as!String)
one.content= (itemDic.value(forKey:"content")as!String)
modelArr.append(one)
}
print(modelArr)
pass(modelArr,true)
}.resume()
}
===================================一级======================
let scrWidth = UIScreen.main.bounds.size.width
let scrHeight = UIScreen.main.bounds.size.height
varrecipeTF:UITextField? // 菜谱输入框
varsearchBtn:UIButton? // 搜索按钮
self.view.backgroundColor = UIColor.yellow
recipeTF=UITextField(frame:CGRect.init(x:0, y:0, width:200, height:50))
recipeTF?.center=CGPoint(x:scrWidth/2, y:200)
recipeTF?.borderStyle= .line
recipeTF?.placeholder="请输入查询"
recipeTF?.textColor = UIColor.blue
recipeTF?.textAlignment= .center
recipeTF?.clearButtonMode = .whileEditing
recipeTF?.keyboardType= .namePhonePad
recipeTF?.delegate=self
self.view.addSubview(recipeTF!)
searchBtn=UIButton(frame:CGRect(x:0, y:0, width:100, height:50))
searchBtn?.center=CGPoint(x:scrWidth/2, y:300)
searchBtn?.setTitle("点击查询", for: .normal)
searchBtn?.backgroundColor = UIColor.black
searchBtn?.setTitleColor(UIColor.white, for: .normal)
searchBtn?.addTarget(self, action:#selector(btnDidPress(sender:)), for: .touchUpInside)
self.view.addSubview(searchBtn!)
@objcfuncbtnDidPress(sender:UIButton) ->Void{
if(recipeTF?.text?.isEmpty)! {
return
}
// 实例化控制器对象
let resultVC = GfdResultViewController()
// 传递数据
resultVC.passString= (recipeTF?.text)!
// 控制器跳转
self.navigationController?.pushViewController(resultVC, animated:true)
}
=================================二级==========================
varpassString:String=""
vartableData:[gfd]?
vartable:UITableView?
overridefuncviewDidLoad() {
super.viewDidLoad()
// 实例化表格视图
table=UITableView.init(frame:self.view.frame, style: .plain)
table?.dataSource =self
table?.delegate=self
table?.rowHeight=100
self.view.addSubview(table!)
self.view.backgroundColor = UIColor.white
self.navigationItem.title = "\"\(passString)\"的搜索结果" // Do any additional setup after loading the view.
}
overridefuncviewWillAppear(_animated:Bool) {
// 请求网络数据
leturlSer =URLService()
urlSer.POST(url:self.passString) { (data, success)in
if!success {
return
}
self.tableData= dataas? [gfd]
DispatchQueue.main.async {
self.table?.reloadData()
}
}
}
functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
ifletcount =tableData?.count{
returncount
}
return0
}
functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
letidentifier ="cell"
varcell = tableView.dequeueReusableCell(withIdentifier: identifier)
ifcell ==nil{
cell =UITableViewCell.init(style: .subtitle, reuseIdentifier: identifier)
}
letone =self.tableData![indexPath.row]as?gfd
cell?.textLabel?.text= one?.province
cell?.detailTextLabel?.text= one?.content
returncell!
}
====================model=================
class gfd: NSObject {
varprovince:String?
varcontent:String?
}