2015-01-03 59 views
0

我請求通過JSON的一些數據,我是新來的SWIFT爲iOS,所以我沒有得到什麼在我的代碼的問題:錯誤而在SWIFT請求JSON + IOS

var URL="http://X.X.X.X.X/Api/UserManagement/getMobileUser"; 
    var UserId=uname.text; 
    var pword=passwd.text; 
    var PostData: NSString = "{\"data\":{},\"action\":\"System\",\"method\":\"getMobileUser\",\"username\":\" mpc01\",\"password\":\"mpc01\",\"type\":\"rpc\",\"tid\":\"144\"}" 
    let request = NSMutableURLRequest(URL: NSURL(string: URL)!) 
    request.HTTPMethod = "POST" 

    let postString = PostData 
    //var st:NSData = PostData.dataUsingEncoding(NSUTF8StringEncoding)! 
    // request.HTTPBody = PostData.dataUsingEncoding(NSUTF8StringEncoding) 

    request.HTTPBody = (postString as NSString).dataUsingEncoding(NSUTF8StringEncoding) 


    //let data = (postString as NSString).dataUsingEncoding(NSUTF8StringEncoding) 
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { 
     data, response, error in 

     if error != nil { 
      println("error=\(error)") 
      return 
     } 

     println("response = \(response)") 


     let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) 
     println("responseString = \(responseString)") 

    } 
    task.resume() 

響應我得到是:

response = {URL:http://X.X.X.X/Api/UserManagement/getMobileUser} {status code:500,headers'Cache-Control'=「no-cache」; 「Content-Length」= 36; 「Content-Type」=「application/json; charset = utf-8」; Date =「Sat,03 Jan 2015 06:11:51 GMT」; Expires =「-1」; Pragma =「no-cache」; Server =「Microsoft-IIS/7.5」; 「X-AspNet-Version」=「4.0.30319」; 「X-Powered-By」=「ASP.NET」; ) }} responseString =可選({「Message」:「發生了錯誤。」})

+0

我假設你已經測試過服務API(使用其他客戶端)並且它可以工作。然後,您創建的HTTP請求不正確。我建議您嘗試將至少另外兩個HTTP頭文件設置爲'application/json'和'Content-Length'的Content-Type到UTF-8編碼數據的長度。 – Codo

+0

好吧我要試一試 –

回答

-1

我有一個類似的問題,嘗試POSTGmail的一些自動化的電子郵件,我在應用程序中實現。

我能夠通過一個大的HTTP響應正常工作。我把完整的路徑放到Keys.plist中,以便我可以將我的代碼上傳到github,並將一些參數分解爲變量,以便我可以在後面以編程方式設置它們。

// Email the FBO with desired information 
// Parse our Keys.plist so we can use our path 
var keys: NSDictionary? 

if let path = NSBundle.mainBundle().pathForResource("Keys", ofType: "plist") { 
    keys = NSDictionary(contentsOfFile: path) 
} 

if let dict = keys { 
    // variablize our https path with API key, recipient and message text 
    let mailgunAPIPath = dict["mailgunAPIPath"] as? String 
    let emailRecipient = "[email protected]" 
    let emailMessage = "Testing%20email%20sender%20variables" 

    // Create a session and fill it with our request 
    let session = NSURLSession.sharedSession() 
    let request = NSMutableURLRequest(URL: NSURL(string: mailgunAPIPath! + "from=FBOGo%20Reservation%20%[email protected]<my domain>.com%3E&[email protected]<my domain>.com&to=\(emailRecipient)&subject=A%20New%20Reservation%21&text=\(emailMessage)")!) 

    // POST and report back with any errors and response codes 
    request.HTTPMethod = "POST" 
    let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in 
     if let error = error { 
      print(error) 
     } 

     if let response = response { 
      print("url = \(response.URL!)") 
      print("response = \(response)") 
      let httpResponse = response as! NSHTTPURLResponse 
      print("response code = \(httpResponse.statusCode)") 
     } 
    }) 
    task.resume() 
} 

的Mailgun路徑是Keys.plist一個名爲mailgunAPIPath與字符串值:

https://API:key-<my key>@api.mailgun.net/v3/<my domain>.com/messages? 

希望這有助於!