03_Alamofire_POST
import UIKit
import Alamofire
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Alamofire第三方庫的POST
/*
1.導(dǎo)入Alamofire庫文件(Alamofire.xcodeproj)
2.項目targets的第一個->General->Embedded binaries->點擊加號,選擇iOS版本的庫
3.import Alamofire
4.使用
*/
//http://api.izhangchu.com/
//methodName=HomeIndex&user_id=0&version=1.0
/*
第一個參數(shù):區(qū)分GET和POST
第二個參數(shù):請求的網(wǎng)址
第三個參數(shù):請求體的參數(shù)
第四個參數(shù):網(wǎng)址的編碼方式
第五個參數(shù):請求頭參數(shù)
*/
let paramDict = ["methodName": "HomeIndex","user_id":"0","version":"1.0"]
Alamofire.request(.POST, "http://api.izhangchu.com/", parameters: paramDict, encoding: ParameterEncoding.URL, headers: nil).responseJSON { (response) in
switch response.result {
case .Failure(let error):
print(error)
case .Success(let jsonData):
print(jsonData)
}
}
// //上傳文件
// /*
// 第一個參數(shù):請求方式
// 第二個參數(shù):請求網(wǎng)址
// 第三個參數(shù):上傳文件的設(shè)置
// 第四個參數(shù):編碼
// */
// Alamofire.upload(.POST, "http://wwww.baidu.com", multipartFormData: { (multipartFormData) in
//
// //上傳圖片
// /*
// 第一個參數(shù):文件的二進制數(shù)據(jù)
// 第二個參數(shù):文件數(shù)據(jù)的key值
// 第三個參數(shù):文件的名字(存儲到服務(wù)器上的文件名)
// 第四個參數(shù):文件類型(image/png)
// */
//
// let path = NSBundle.mainBundle().pathForResource("1", ofType: "png")
// let data = NSData(contentsOfFile: path!)
//
// multipartFormData.appendBodyPart(data: data!, name: "headimage", fileName: "h.png", mimeType: "image/png")
//
//
// //上傳其他文件
// let path2 = NSBundle.mainBundle().pathForResource("shanghai", ofType: "word")
// let url = NSURL(fileURLWithPath: path2!)
// multipartFormData.appendBodyPart(fileURL: url, name: "info")
//
// }) { (result) in
//
// }
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。