开发了这些年,做为一个颇有些经验的开发人员来说,我不是很喜欢用别人写的库。除非比较复杂。简单的库还是喜欢自己完成。可以量身打造,随便改动。今天继续贴出来HTTP封装类库,大家相互学习交流下。
HttpClient.swift
HttpResult.swift
下面是VC中的点击代码。调用。
也很简单,对吧!
HttpClient.swift
import UIKit class HttpClient: NSObject { static let TIME_OUT:NSTimeInterval = 10 private static func initRequest(urlString :String)->NSURLRequest! { let url = NSURL(string: urlString) let request = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData, timeoutInterval: TIME_OUT) return request } private static func initMutableURLRequest(urlString :String)->NSMutableURLRequest! { let url = NSURL(string: urlString) let request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData, timeoutInterval: TIME_OUT) return request } static func httpAsynRequest(urlString :String, completionHandler: (HttpResult!) -> Void){ let request = initRequest(urlString) NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response:NSURLResponse?, data:NSData?, error:NSError?) -> Void in if error != nil { completionHandler(HttpResult(error: error!)) }else if response != nil { let httpResponse = response as! NSHTTPURLResponse let httpResult = HttpResult(responseCode: httpResponse.statusCode,data: data!) completionHandler(httpResult) } } } static func httpAsyncPostStringRequest(urlString :String,postData :String, completionHandler: (HttpResult!) -> Void){ httpAsyncPostRequest(urlString, postData: postData.dataUsingEncoding(NSUTF8StringEncoding)!, completionHandler: completionHandler) } static func httpAsyncPostRequest(urlString :String,postData :NSData, completionHandler: (HttpResult!) -> Void){ let request = initMutableURLRequest(urlString) request.HTTPMethod = "POST" request.HTTPBody = postData request.addValue("application/json", forHTTPHeaderField: "Content-Type") let config = NSURLSessionConfiguration.defaultSessionConfiguration()//默认配置 config.timeoutIntervalForRequest = TIME_OUT //连接超时时间 let session = NSURLSession(configuration: config) let task = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in if error != nil { completionHandler(HttpResult(error: error!)) }else if response != nil { let httpResponse = response as! NSHTTPURLResponse let httpResult = HttpResult(responseCode: httpResponse.statusCode,data: data!) completionHandler(httpResult) } } task.resume() } static func httpPostBinRequest(urlString :String,postData :NSData, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void){ let request = initMutableURLRequest(urlString) request.HTTPMethod = "POST" request.HTTPBody = postData let config = NSURLSessionConfiguration.defaultSessionConfiguration()//默认配置 config.timeoutIntervalForRequest = TIME_OUT //连接超时时间 let session = NSURLSession(configuration: config) let task = session.dataTaskWithRequest(request,completionHandler: completionHandler) task.resume() } }
HttpResult.swift
import Foundation class HttpResult: NSObject { private var responseCode:Int! private var responseString:String! private var data:NSData! private var error:NSError! init(error:NSError) { self.responseCode = 0 self.responseString = nil self.data = nil self.error = error } init(responseCode:Int,data:NSData) { self.responseCode = responseCode self.data = data } func getResponseCode()->Int { return self.responseCode } func getResponseString()-> String { if self.responseString == nil { self.responseString = NSString(data:data!, encoding: NSUTF8StringEncoding) as String! } return self.responseString } func getData()->NSData { return self.data } func isSuccess()-> Bool { return self.responseCode == 200 } }
下面是VC中的点击代码。调用。
@IBAction func loginClicked(sender: UIButton) { HttpClient.httpAsynRequest("http://www.it72.com/json.php") { (result) -> Void in if result.isSuccess() { JsonParser.parse(result.getData()) self.presentViewController(self.sideMenuConvtroller, animated: true, completion: nil) }else{ print("登录失败") } } }
也很简单,对吧!
收藏的用户(0) X
正在加载信息~
推荐阅读
APP清除缓存功能的具体实现
最新回复 (0)
站点信息
- 文章2302
- 用户1336
- 访客10970171
每日一句
Qingming Festival invites us to honor ancestors with quiet reflection and respect.
清明节邀请我们以静思与敬意祭奠祖先。
清明节邀请我们以静思与敬意祭奠祖先。
新会员