http get(swift and oc)

來源:互聯網
上載者:User

標籤: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: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=[NSStringstringWithFormat:@"http://www.weather.com.cn/data/sk/101010100.html"];

    NSURL *url=[NSURLURLWithString:urlStr];

    //    2.建立請求對象

    NSURLRequest *request=[NSURLRequestrequestWithURL:url];

    NSData *received = [NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror:nil];

    NSString *str = [[NSStringalloc]initWithData:receivedencoding: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)

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.