我是swift的新手,我试图在Facebook的用户墙上发布新的Feed,但是我的api响应在控制台中显示为nil,并且在网络上工作正常,任何帮助都表示赞赏,谢谢。
@IBAction func upload(_ sender: AnyObject) {
let user = UserManager.getLoggedInUser()!
let request = NSMutableURLRequest(url:NSURL(string: "http://www.MYAPI")!as URL)
let session = URLSession.shared
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let PostUtctime = DateFormatter()
PostUtctime.dateFormat = "YYYY-MM-dd'To'HH:mm:ss"
let str: String = PostUtctime.string(from: Date())
print("Date = \(str)")
let guidMemberID = user.userID
print(guidMemberID)
let paramToSend = "{\"guidMemberID\":\"\(user.userID)\", \"ImageString\": \", \"strAboutFeed\":\"\(textView.text)\", \"PostUtctime\": \"\(PostUtctime)\"}"
API通常在执行上述行后说明其成功发布或发生错误
print(textView.text as Any)
request.httpBody = (paramToSend as AnyObject) as? Data
session.dataTask(with: request as URLRequest,completionHandler: {
(data, response, error) -> Void in
guard let _:Data = data
else {
print(response as Any)
return
}
var json: [String:AnyObject]
do {
json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary as! [String : AnyObject]
print(json)
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
// check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseDataDictionary: Dictionary<String,AnyObject> = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! Dictionary<String,AnyObject>
print(responseDataDictionary)
}
catch {
print("Error: \(error.localizedDescription)")
}
}).resume()
}
}
这是,如果代码中有错误部分,感谢您抽出时间。