iOS HTTP请求
来源:互联网 发布:领航网络电商有限公司 编辑:程序博客网 时间:2024/06/01 08:46
没有容错处理,没有降耦合设计、接口多级封装等等的设计
原生NSURLSession做个http请求很简单啊(swif代码看的真心顺眼多了)
import UIKitimport Foundationclass LLNetwork: NSObject { func request(method: String, url: String, params: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>(),callback: (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void) { //组装URL var newURL = url if method == "GET"{ newURL += "?" newURL += self.buildParams(params) } //创建URLRequest 存储请求信息 let request = NSMutableURLRequest(URL: NSURL(string: url)!) request.HTTPMethod = method if method == "POST"{ request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.HTTPBody = buildParams(params).dataUsingEncoding(NSUTF8StringEncoding) } //创建网络请求会话 let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in callback(data: data, response: response , error: error) }) task.resume() } //组合参数 func buildParams(params: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()) -> String { var newURL = "" for (key,value) in params{ newURL += (key + "=" + (value as! String) + "&") } newURL = newURL.substringToIndex(newURL.characters.indexOf(newURL.characters.last!)!) return newURL }}
0 0
- ios http请求
- IOS之HTTP请求
- ios http请求
- ios HTTP post请求
- ios http请求
- iOS HTTP请求
- IOS Http请求
- iOS http 请求
- IOS-HTTP请求
- ios 添加 http 请求
- iOS发送http请求
- iOS 允许HTTP请求
- iOS网络--HTTP请求
- IOS http网络请求
- iOS iOS9 HTTP请求失败
- iOS NsSession http请求错误
- iOS HTTP请求与解析
- iOS http请求记录一下
- 文章标题
- linux 性能优化
- Android动画的实现原理
- SPI中的极性CPOL和相位CPHA是什么以及如何设置
- android源码
- iOS HTTP请求
- <转>Objective-C开发 从java转向Objective-C思想转变总结
- 上拉加载,下拉刷新
- android ViewPager实现广告轮播
- Android 中String的小秘密
- javax.validation.ValidationException: HV000183: Unable to load 'javax.el.ExpressionFactory'
- Android中用到的设计模式
- xcode打包报错:there was an internal api error
- iOS UIWindow,UIApplication类中的属性介绍