码迷,mamicode.com
首页 > Web开发 > 详细

HttpRequest

时间:2016-04-07 20:25:04      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

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)
})
}
}
}
}

HttpRequest

标签:

原文地址:http://www.cnblogs.com/to-creat/p/5365097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!