首页 > Web开发 > 详细

HttpRequest

时间:2016-04-07 20:23:21      阅读:275      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!