HttpRequest
class HttpRequest: NSObject{
override init() {
super.init()
}
class func parseJSONData(data: AnyObject?) ->NSArray?{
do{
let downloadData=data as? NSData
let jsonObject = try NSJSONSerialization.JSONObjectWithData(downloadData!, options: NSJSONReadingOptions.MutableContainers) as? NSArray
return jsonObject
}catch{
}
return nil
}
class func request(urlString urlString: String?,completion: (data: NSArray?) -> Void){
if urlString == nil {
dispatch_async(dispatch_get_main_queue(),{ () -> Void in
print("urlString 为空")
completion(data: nil)
})
}
let url = NSURL(string: urlString!)
let request = NSURLRequest(URL: url!)
let queue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: queue) {
(response, data, error) -> Void in
let httpResponse = response as? NSHTTPURLResponse
let code=httpResponse?.statusCode
if code==200{
//NSLog("123","123")
let parseData = self.parseJSONData(data)
if parseData == nil{
NSLog("123", "debug")
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
completion(data: parseData)
})
}else{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print(error)
// 请求出现,则返回nil对象表示
completion(data: nil)
})
}
}
}
}