无法Json响应无法正常工作

时间:2017-12-15 11:01:59

标签: ios swift

Swift 3.0(Xcode 8.3)

我正在尝试创建一个小程序,将用户名发送到Web数据库。我找到了如何发送first_namelast_namedobowner_mobileowner_email, 通过在POST方法中使用owner_passwordchoice_for_verification。我之前获得了一个JSON字符串,然后当我尝试解析它时,我在try NSJSONSerialization行上的as关键字上得到了上述错误。我做错了什么?谢谢你的回答。

@IBAction func Login_Action(_ sender: Any)
{
    var responseString : String!
    var request = URLRequest(url: URL(string: "http://dev.justpick2go.com/cpanel/api/owner/ownerregistration.php")!)
    request.setValue("Application/x-www.ownerregistration.php.com", forHTTPHeaderField: "Content-Type")
    request.httpMethod = "POST"
    let postString = "first_name=\(txtFirstName.text!)&last_name=\(txtLastName.text!)&dob=\(txtDOB.text!)&owner_mobile=\(txtMobileNo.text!)&owner_email=\(txtEmailID.text!)&owner_password=\(txtPassword.text!)" //&choice_for_verification=\(email)"            // sending a parameters

    print("\(postString)")
    request.httpBody = postString.data(using: .utf8)
   let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
    guard let data = data, error != nil else {                  //checking for fundamental error
        print("Error is =\(String(describing: error))")
        return
    }
    if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200
    {                                                                                       // checking for http errors
        print("statusCode should be 200 , but is\(httpStatus.statusCode)")
        print("response is =\(String(describing: response))")
    }
    responseString = String(data: data, encoding: .utf8)
    print("ResponseString=\(responseString!)")
    do {
        let json : NSDictionary! = try! JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSDictionary
        self.parseTheJSonData(JsonData: json)
    }
    catch
    {
        print(error)
    }
    }
    task.resume()
}
func parseTheJSonData(JsonData : NSDictionary)
{
    var successMessage : String = String()
    var sampleCode : Int = Int()

    let verificationAlert = UIAlertController()
    if ((JsonData.value(forKey: "success") as! Int) == 1)
    {
        successMessage = "Login is Successful"
        sampleCode = JsonData.value(forKey: "success") as! Int
        verificationAlert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
        verificationAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (Relogin) in
            let Log = self.storyboard?.instantiateViewController(withIdentifier: "") as! LoginViewController
            self.navigationController?.pushViewController(Log, animated: true)
            self.present(Log, animated: true, completion: nil)
        }))
    }
    else if ((JsonData.value(forKey: "success") as! Int) == 0)
    {
        sampleCode = JsonData.value(forKey: "success") as! Int
        successMessage = "Please try again"
        verificationAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
    }
    verificationAlert.title = successMessage

    OperationQueue.main.addOperation
        {
            self.present(verificationAlert, animated: true, completion: nil)
        }

}

0 个答案:

没有答案