2017-11-02 5 views
1

テキストフィールドに書き込まれたjsonにデータを保存できません。保存ボタンをクリックすると、更新されたデータをjsonに保存する必要があります。 は、ここで私はボタンアクションを保存するに書かれているコードです:ポストデータコードのポストを使用して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)")!) 
     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() 
     } 
+0

httpStatusコードの記録を表示できますか?そして、テストする1サンプルのNgoIdキーをつけてください。 – Warewolf

答えて

0

てみてください、次のように、これはあなたを助けるでしょう。そして、try-catchを使ってエラーを取得してみてください。以下はあなたの作業コードです。

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() 
    } 

ログのレスポンス-----

{ 
    listngoprofile =  (
       { 
      AboutNGO = "<null>"; 
      City = "<null>"; 
      CityofRegisteration = "<null>"; 
      CompleteAddress = "<null>"; 
      Country = "<null>"; 
      DateofRegisteration = "<null>"; 
      Declaration = 1; 
      EmailID = "[email protected]"; 
      FCRADateofRegisteration = ""; 
      FCRARegisterationNo = ""; 
      FCRAValidUpto = "19/07/2025"; 
      "Fax_No" = "<null>"; 
      Issues =    (
           { 
        Issues = "Education& Scholarship"; 
       }, 
           { 
        Issues = fdfsdfasdfdsd; 
       }, 
           { 
        Issues = kdjkdsksd; 
       }, 
           { 
        Issues = dfjkkdf; 
       } 
      ); 
      Majorachievements = "<null>"; 
      MobileNo = 9922400698; 
      NGOBankAccountNo = "<null>"; 
      NGOBankAccounttype = "<null>"; 
      NGOBankIFSCCode = "<null>"; 
      NameinBankAccount = "<null>"; 
      NameofChairman = "<null>"; 
      NameofChiefFunctionary = "<null>"; 
      NameofSeceratary = "<null>"; 
      NameofTreasurer = "<null>"; 
      Nameofngo = "<null>"; 
      NgoID = 1; 
      ParentOrganization = "<null>"; 
      Pincode = "<null>"; 
      Regcertimg = "<null>"; 
      RegisterationNo = "<null>"; 
      Registeredwith = "<null>"; 
      State = "<null>"; 
      StateofRegisteration = "<null>"; 
      "TelePhone_No" = "<null>"; 
      TypeofNGO = "<null>"; 
      Validupto = "<null>"; 
      WebsiteURL = "<null>"; 
      YearofEstablishment = "<null>"; 
      keyOperational =    (
           { 
        Area = Maharashtra; 
       }, 
           { 
        Area = dfsdfdfsdfsdfdsfdsfdsfds; 
       }, 
           { 
        Area = kjdjdkjdfjl; 
       }, 
           { 
        Area = jldklk; 
       } 
      ); 
      photoofNGO = "<null>"; 
     } 
    ); 
    success = 1; 
} 

あなたが反応して<null>値のチェックを適用したい場合は、チェックのためのコードの下に従ってください。

if let successVal = response?["success"] as? NSNumber { 
     uWillGetValueHere = successVal; 
    } 
+0

実際に値を設定していないのですが、私は自分のアプリにログインしているときにIDを変数に既に保存していますが、ポストストリングでキャッチしたテキストフィールドは "null"になっています –

+0

あなたのid変数を使用して、私はまた、次の行、UserDefaults.standard.setValue(1、forKey: "NgoId")を使用してID変数を使用しています。 – Warewolf

関連する問題