简体   繁体   中英

Swift Error : Error Domain=NSCocoaErrorDomain Code=3840 “ Problems with my Login Controller ”

This is my code and I am unable to compile it . I am trying to login to my server but it doesnt allow me so Sorry I am new at programming and I've researched on stackoverflow, regarding this error , I can only parse Dictionary or Array. but I've googled by copying my JSON response but it doesnt work. Any Suggestions would be helpful !!

import UIKit

class Login: UIViewController {

@IBOutlet var Username: UITextField!
@IBOutlet var Password: UITextField!



@IBAction func Login(sender: UIButton) {


    let username=Username.text
    let password=Password.text

    let URL_LOGIN="http://172.22.95.116/SoilCondition/app/getLogin.php?USERNAME=" + username! + "&PASSWORD=" + password!;

    let requestURL = NSURL(string: URL_LOGIN)

    let request = NSMutableURLRequest(URL: requestURL!)

    request.HTTPMethod = "POST"



    let postParameters = "username="+username!+"&password="+password!;

    request.HTTPBody =            postParameters.dataUsingEncoding(NSUTF8StringEncoding)
    let session = NSURLSession.sharedSession()


    let task = session.dataTaskWithRequest(request){
        data, response, error in guard let data = data where error == nil
            else {
                print("error: \(error)")
                return
        }

        do{
            let myJSON = try NSJSONSerialization.JSONObjectWithData(data,  options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
            if let parseJSON = myJSON{

                var msg: String!
                msg = parseJSON["message"] as! String?
                print(msg)
            }

           /* if let parseJSON = myJSON {

                var msg : String!

                msg = parseJSON["message"] as! String?

                print(msg)
            }*/
            /*if data != nil {
                json = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
                println("json: \(json)")

                if let dictionary = parseJSON(jsonString) {
                    println("dictionary: \(dictionary)")
                }*/
        } catch let parseError{
            print(parseError)
            }
        }
    task.resume()
}



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

I found this question regarding error code 3840.

As it says there, the problem could be that your server doesn't return valid JSON to you.

Now, you say:

I can only parse Dictionary or Array

I don't know if that means that you are able to actually parse the response you receive from the server into valid JSON here:

let myJSON = try NSJSONSerialization.JSONObjectWithData(data,  options: NSJSONReadingOptions.MutableContainers) as? NSDictionary

But if it doesn't then a good place to start could be to verify whether your server actually returns valid JSON to you.

To do so, you could try calling your server directly from cURL or postman and see what you get in return.

Hope that helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM