码迷,mamicode.com
首页 > 编程语言 > 详细

http get(swift and oc)

时间:2015-07-23 00:41:49      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:ios

       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: NSDataNSURLConnection.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"}}




版权声明:本文为博主原创文章,未经博主允许不得转载。

http get(swift and oc)

标签:ios

原文地址:http://blog.csdn.net/u011636207/article/details/47012417

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!