在这里,你走了,我的机器上测试了当地的网络服务
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: NSURL(string: "http://localhost:8080/iOSServer/ios/helloworld/getJSONArray")!)
request.HTTPMethod = "GET" // change it to post if you want
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
if let error = error {
print(error)
}
if let data = data{
do{
let resultJSON = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
let resultArray = resultJSON as? NSArray
for jsonObjectString in resultArray!{
let platform = jsonObjectString["platform"]!
let favorite = jsonObjectString["favorite"]!
let language = jsonObjectString["language"]!
print("platform = \(platform!)")
print("favorite = \(favorite!)")
print("language = \(language!)")
}
}catch _{
print("Received not-well-formatted JSON")
}
}
if let response = response {
let httpResponse = response as! NSHTTPURLResponse
print("response code = \(httpResponse.statusCode)")
}
})
task.resume()
}
如你所见,for
循环中,我打印自带形成服务器的值,你可以随心所欲地做任何事情。
我认为JSON密钥是:platform
,favorite
和language
您可以更改它们。 (请照顾大写字母和小写字母)
非常感谢! –
嗨,虽然我仍然在同一个项目上工作,但我注意到在调试过程中它退出程序后'let task = session.dataTaskWithRequest(request,completionHandler:{(data,response,error)in',它执行的地方''task.resume()'不通过'If'语句。 由于这个问题,我没有提取并显示在我的UITableView。 –
@JoeLeong请创建一个新问题,描述新问题并发送给我链接 –