【发布时间】:2015-12-30 16:16:32
【问题描述】:
我正在尝试通过 Alamofire 发送简单的 http post 请求。我需要用户名和密码,请求正文是 json 格式。 首先,我不确定我的代码是否正确(Apple 开发人员中的初学者。) 其次,每次我激发代码时我都很困惑,它根本没有进入 responseJSON 块。谁能给我一个提示,谢谢 这是我的代码
func sendTestResult(result: Int, testrun: Int, testcaseindex: Int) -> Bool{
var resp : Bool = false;
//add testcase info
var url: String = self.testrailsUrl + "/\(testrun)/\(testcaseindex)"
//add authentication info
// url = testrailsUrl + "/basic-auth/\(self.testrailsUserName)/\(self.testrailsPassword)"
assert(url == "http://ypdine.testrail.net/index.php?/api/v2/add_result_for_case/4/3",
"the post information isn't right")
let parameters = ["status_id": [result]]
print("here sending the request")
Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON)
.authenticate(user: self.testrailsUserName, password: self.testrailsPassword)
.validate(statusCode: 200..<300)
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
return resp
}
【问题讨论】:
-
是真的没有进入block,还是在进入
response.result.value只是nil。检查response.result.error。 -
嗨@Rob先谢谢你的关注,真的是进不去块,我设置了断点,一步一步走。打到了.responseJSON这一行,直接跳转返回
标签: json swift http post alamofire