Swift3 无法使用post将数据保存到json
我无法将数据保存回写在文本字段中的json,当单击save按钮时,必须将更新的数据保存到json。 以下是我编写的关于保存按钮操作的代码:Swift3 无法使用post将数据保存到json,swift3,Swift3,我无法将数据保存回写在文本字段中的json,当单击save按钮时,必须将更新的数据保存到json。 以下是我编写的关于保存按钮操作的代码: if let id = UserDefaults.standard.string(forKey: "NgoId"){ var request = URLRequest(url: URL(string: "http://www.shreetechnosolution.com/funded/ngo_profile.php?ngoid=\(id)"
if let id = UserDefaults.standard.string(forKey: "NgoId"){
var request = URLRequest(url: URL(string: "http://www.shreetechnosolution.com/funded/ngo_profile.php?ngoid=\(id)")!)
request.httpMethod = "POST"
let postString = "Nameofngo=\(textngoname.text!)&NameofChiefFunctionary=\(textcheiffunction.text!)&NameofChairman=\(textchairman.text!)&NameofSeceratary=\(textsecratary.text!)&NameofTreasurer=\(texttreasurer.text!)&ParentOrganization=\(textparentorg.text!)&AboutNGO=\(Textngo.text)&photoofNGO=\(imageviewngo.image)&ngoid=\(id)&value=NGO"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error ?? "" as! Error)")
return
}
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 json = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
let msg = json.value(forKey: "message") as! NSString!
let alert : UIAlertView = UIAlertView(title: "Alert box!", message: "\(msg!).",delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
task.resume()
}
尝试以下方式发布数据代码,这将对您有所帮助。并始终尝试使用捕捉来获取错误。下面是您的工作代码和响应
UserDefaults.standard.setValue(1, forKey: "NgoId");
if let id = UserDefaults.standard.string(forKey: "NgoId"){
var request = URLRequest(url: URL(string: "http://www.shreetechnosolution.com/funded/ngo_profile.php?ngoid=\(id)")!)
request.httpMethod = "POST"
var postJson = Dictionary<String, Any>.init();
postJson["Nameofngo"] = "NONAME";
postJson["NameofChiefFunctionary"] = "";
postJson["NameofChairman"] = "";
postJson["NameofSeceratary"] = "";
postJson["NameofTreasurer"] = "";
postJson["ParentOrganization"] = "";
postJson["AboutNGO"] = "";
postJson["ngoid"] = id;
postJson["value"] = "NGO";
print("post json \(postJson)");
do {
let postData = try JSONSerialization.data(withJSONObject: postJson, options: JSONSerialization.WritingOptions.prettyPrinted);
request.httpBody = postData;
}catch let err {
print("exception is \(err)");
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let responseData = data, error == nil else { // check for fundamental networking error
print("error=\(error ?? "" as! Error)")
return
}
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 json = try! JSONSerialization.jsonObject(with: responseData, options: []) as! NSDictionary
print("json response \(json)");
}
task.resume()
}
尝试以下方式发布数据代码,这将对您有所帮助。并始终尝试使用捕捉来获取错误。下面是您的工作代码和响应
UserDefaults.standard.setValue(1, forKey: "NgoId");
if let id = UserDefaults.standard.string(forKey: "NgoId"){
var request = URLRequest(url: URL(string: "http://www.shreetechnosolution.com/funded/ngo_profile.php?ngoid=\(id)")!)
request.httpMethod = "POST"
var postJson = Dictionary<String, Any>.init();
postJson["Nameofngo"] = "NONAME";
postJson["NameofChiefFunctionary"] = "";
postJson["NameofChairman"] = "";
postJson["NameofSeceratary"] = "";
postJson["NameofTreasurer"] = "";
postJson["ParentOrganization"] = "";
postJson["AboutNGO"] = "";
postJson["ngoid"] = id;
postJson["value"] = "NGO";
print("post json \(postJson)");
do {
let postData = try JSONSerialization.data(withJSONObject: postJson, options: JSONSerialization.WritingOptions.prettyPrinted);
request.httpBody = postData;
}catch let err {
print("exception is \(err)");
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let responseData = data, error == nil else { // check for fundamental networking error
print("error=\(error ?? "" as! Error)")
return
}
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 json = try! JSONSerialization.jsonObject(with: responseData, options: []) as! NSDictionary
print("json response \(json)");
}
task.resume()
}
你能告诉我你的httpStatus代码是什么吗。并给我一个示例NgoId密钥进行测试。你能告诉我你的httpStatus代码是什么吗。并给我一个示例NgoId密钥进行测试。事实上,我没有在这里设置值,我希望在登录我的应用程序时使用该id,我已经将其存储在id变量中,并且您在poststring“”中检查为null的文本字段具有值,以防发生什么情况,然后可以使用您的id变量,我还通过使用以下行使用您的id变量,UserDefaults.standard.setValue(1,forKey:“NgoId”);实际上,我没有在这里设置值,我想在我登录我的应用程序时使用该id,我已经将其存储在id变量中,并且您在poststring“”中检查为null的文本字段中有值,以防发生什么情况。使用您的id变量,我也使用您的id变量,使用以下行,UserDefaults.standard.setValue(1,forKey:“NgoId”);
if let successVal = response?["success"] as? NSNumber {
uWillGetValueHere = successVal;
}