http get(swift and oc)
SynchonousRequest:
let urlPath: String = "http://www.weather.com.cn/data/sk/101010100.html"
var url: NSURL = NSURL(string: urlPath)!
var request1: NSURLRequest = NSURLRequest(URL: url)
var response: AutoreleasingUnsafeMutablePointer<NSURLResponse? >= nil
var error: NSErrorPointer = nil
var dataVal: NSData = NSURLConnection.sendSynchronousRequest(request1, returningResponse: response, error:nil)!
var err: NSError
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
println("Synchronous \(jsonResult)")
Log:
Synchronous {
weatherinfo = {
Radar = "JC_RADAR_AZ9010_JB";
SD = "26%";
WD = "\U4e1c\U5357\U98ce";
WS = "2\U7ea7";
WSE = 2;
city = "\U5317\U4eac";
cityid = 101010100;
isRadar = 1;
njd = "\U6682\U65e0\U5b9e\U51b5";
qy = 1012;
temp = 10;
time = "10:25";
};
}
// 1.设置请求路径
NSString *urlStr=[NSString stringWithFormat:@"http://www.weather.com.cn/data/sk/101010100.html"];
NSURL *url=[NSURL URLWithString:urlStr];
// 2.创建请求对象
NSURLRequest *request=[NSURLRequest requestWithURL:url];
NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
Log:
{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"10","WD":"东南风","WS":"2级","SD":"26%","WSE":"2","time":"10:25","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1012"}}