同步Get方式
1 // MARK: - 同步Get方式 2 func synchronousGet() 3 { 4 //创建NSURL对象 5 var url:NSURL! = NSURL(string: "http://m.weather.com.cn/data/101010100.html") 6 7 //创建请求对象 8 var urlRequest : NSURLRequest = NSURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 10) 9 10 //响应对象 11 var response:NSURLResponse? 12 13 //错误对象 14 var error:NSError? 15 16 //发出请求 17 var data:NSData? = NSURLConnection.sendSynchronousRequest(urlRequest, returningResponse: &response, error: &error) 18 19 if (error != nil) 20 { 21 println(error?.code) 22 println(error?.description) 23 }else 24 { 25 var jsonString = NSString(data: data!, encoding: NSUTF8StringEncoding) 26 27 println(jsonString) 28 } 29 30 }
时间: 2024-12-25 16:07:49