我想从服务器下载json文件并解析它
当在下载函数中运行成功回调时,我使用fileExistsAtPath
函数来查找文件是否在位置,结果是否在
我使用swiftHTTP来实现下载功能
这是我的代码
func download(url: String, parameters: Dictionary<String,AnyObject>?,success:((String) -> Void)!, failed:(() -> Void)!, errord:((NSError) -> Void)!) {
var request = HTTPTask();
let downloadTask = request.download(url, parameters: parameters, progress: {(complete: Double) in
}, success: {(response: HTTPResponse) in
if response.responseObject != nil {
//we MUST copy the file from its temp location to a permanent location.
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let str = "\(paths[0])/\(response.suggestedFilename!)"
let newPath = NSURL(string: str)!
let fileManager = NSFileManager.defaultManager()
fileManager.removeItemAtURL(newPath, error: nil)
fileManager.moveItemAtURL(response.responseObject! as NSURL, toURL: newPath, error: nil)
var isDir:ObjCBool = false
let exits = fileManager.fileExistsAtPath(path, isDirectory: &isDir)
if isDir {
println("file is in")
}else{
// I can not found the file in path
println("file is not in")
}
success(str)
}
} ,failure: {(error: NSError, response: HTTPResponse?) in
errord(error)
})
}
@IBAction func login(sender: AnyObject) {
download("XXXXXXXXXXXXXXX", parameters: nil, success: { (path:String) -> Void in
println("\(path)")
var data = NSData(contentsOfFile: path)
var error:NSError?;
var playData = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves, error: &error) as? NSArray
var arrayCount = playData!.count
println("\(arrayCount)")
}, failed: { () -> Void in
}) { (error:NSError) -> Void in
}
}
答案 0 :(得分:0)
我已经解决了 这是我的代码
public func download(url: String, parameters: Dictionary<String,AnyObject>?,success:((NSURL) -> Void)!, failed:(() -> Void)!, errord:((NSError) -> Void)!) {
var request = HTTPTask();
let downloadTask = request.download(url, parameters: parameters, progress: {(complete: Double) in
println("progress:\(complete)")
}, success: {(response: HTTPResponse) in
if response.responseObject != nil {
var fileManager = NSFileManager.defaultManager()
let paths = fileManager.URLsForDirectory( NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
let pathsURL = paths[0] as NSURL
let newPath = pathsURL.URLByAppendingPathComponent(response.suggestedFilename!)
if fileManager.fileExistsAtPath(newPath.path!) {
fileManager.removeItemAtPath(newPath.path!, error: nil)
}
var error:NSError?
var sucess = fileManager.copyItemAtURL(response.responseObject! as NSURL, toURL: newPath, error: &error)
if error != nil {
println("copy error : \(error)")
}
if sucess {
success(newPath)
}else{
failed()
}
}
} ,failure: {(error: NSError, response: HTTPResponse?) in
errord(error)
})
}
request.download("http://demo.yousi.com/Public/file/data.json", parameters: nil, success: { (url:NSURL) -> Void in
var data = NSData(contentsOfURL: url)
var error:NSError?;
var playData = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves, error: &error) as? NSArray
println("playDataCount:\(playData?.count)")
}, failed: { () -> Void in
}) { (error:NSError) -> Void in
}