使用Swift从DB获取信息

时间:2015-10-29 14:06:05

标签: json swift

我想从数据库中获取信息,使用PHP&迅速。

程序在此代码的最后一行停止:

let post:NSString = "email=\(username)&password=\(password)"

NSLog("PostData: %@",post);

let url:NSURL = NSURL(string: "http://tatouapp.netau.net/login.php")!

let postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!

let postLength:NSString = String( postData.length )

let request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")


var reponseError: NSError?
var response: NSURLResponse?

let urlData: NSData? = try! NSURLConnection.sendSynchronousRequest(request, returningResponse:&response)

if ( urlData != nil ) {
    let res = response as! NSHTTPURLResponse!;

    NSLog("Response code: %ld", res.statusCode);

    if (res.statusCode >= 200 && res.statusCode < 300)
    {
        let responseData:NSString  = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

        NSLog("Response ==> %@", responseData);

        let error: NSError?

        let jsonData = try! NSJSONSerialization.JSONObjectWithData(urlData!, options: NSJSONReadingOptions.MutableContainers) as! NSArray

我收到此错误消息:

  

致命错误:'试试!'表达式意外地引发了一个错误:错误Domain = NSCocoaErrorDomain Code = 3840“JSON文本没有以数组或对象开头,并且选项允许未设置片段。” UserInfo = {NSDebugDescription = JSON文本不是以数组或对象开头,而是选项允许未设置片段。

0 个答案:

没有答案