HTTP GET (Swift and OC)

Source: Internet
Author: User

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 c23>) ")

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. Set Request Path

nsstring *urlstr=[nsstringstringwithformat: @ "http://www.weather.com.cn/data/sk/101010100.html"];

nsurl *url=[nsurlurlwithstring: urlstr];

//2. Create Request Object

nsurlrequest *request=[nsurlrequestrequestwithurl: URL ];

nsdata *received = [nsurlconnectionsendsynchronousrequest : Request returningresponse:nilerror : Nil ];

nsstring *str = [[nsstringalloc]initwithdata : Receivedencoding:nsutf8stringencoding];

NSLog(@ "%@", str);

Log:

{"Weatherinfo": {"City": " Beijing ", "Cityid": "101010100", "temp": "Ten", "WD": " Southeast Wind "," WS ":" level 2 "," SD ":" 26% "," WSE ":" 2 "," Time ":" 10:25 "," Isradar ":" 1 "," Radar ":" JC_RADAR_AZ9010_JB "," NJD ":" no live "," qy ":" 1012 "}}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HTTP GET (Swift and OC)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.