IOS Network parsing

Source: Internet
Author: User

Network parsing synchronous asynchronous

/*------------------------Get sync-------------------------------------*/

-(Ibaction) GET_TB: (ID) sender

{

1. Create a URL

Nsurl *url = [Nsurl urlwithstring:@ "http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];

2. Create a Request object

Nsmutableurlrequest *requst = [Nsmutableurlrequest Requestwithurl:url];

Request method

[Requst sethttpmethod:@ "get"];

Nsurlresponse *response = nil;

Nserror *err= Nil;

3. Establish a connection

NSData *data = [nsurlconnection sendsynchronousrequest:requst returningresponse:&response error:&err];

NSLog (@ "%@", data);

NSLog (@ "sync");


}

/*---------------------------Get async------------------------------------*/


-(Ibaction) GET_YB: (ID) sender

{

1. Create a URL

Nsurl *url = [Nsurl urlwithstring:@ "http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];

2. Create a Request object

Nsurlrequest *requset = [Nsurlrequest Requestwithurl:url];

3. Establish a connection

[Nsurlconnection sendasynchronousrequest:requset queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {

NSLog (@ "%@", data);

}];

NSLog (@ "async");

}


/*------------------------------Post sync------------------------------------------*/


-(Ibaction) POST_TB: (ID) sender

{

1. Create a URL

Nsurl *url = [Nsurl urlwithstring:@ "Http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];

2. Create a Request object

Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];

[Request sethttpmethod:@ "POST"];

3. Add the package body

NSString *str [email protected] "date=20131129&startrecord=5&len=5&udid=1234567890&terminaltype= iphone&cid=215 ";

Convert string to NSData format

NSData *databody = [str datausingencoding:nsutf8stringencoding];

4. Set the body for the request

[Request Sethttpbody:databody];

5. Create a connection

NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];

NSLog (@ "%@", data);

NSLog (@ "Post sync");

}

/*------------------------------Post Async------------------------------------------*/


-(Ibaction) POST_YB: (ID) sender

{

1. Create a URL

Nsurl *url = [Nsurl urlwithstring:@ "Http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];

2. Create a Request object

Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];

[Request sethttpmethod:@ "POST"];

3. Add body to request object

NSString *bodystr = @ "date=20131129&startrecord=5&len=5&udid=1234567890&terminaltype=iphone& cid=215 ";

NSData *databody = [Bodystr datausingencoding:nsutf8stringencoding];

[Request Sethttpbody:databody];

4. Create a connection

[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {

NSLog (@ "%@", data);

}];

NSLog (@ "post async");

}

/*------------------------GET sync-------------------------------------*/-(ibaction) GET_TB: (ID) sender{//1. Create URL    Nsurl *url = [Nsurl urlwithstring:@ "http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];    2. Create a Request object nsmutableurlrequest *requst = [Nsmutableurlrequest Requestwithurl:url];    Request mode [Requst sethttpmethod:@ "get"];    Nsurlresponse *response = nil;    Nserror *err= Nil; 3. Establish connection NSData *data = [nsurlconnection sendsynchronousrequest:requst returningresponse:&response Error:&err    ];    NSLog (@ "%@", data);    NSLog (@ "sync"); }/*---------------------------GET Asynchronous------------------------------------*/-(ibaction) GET_YB: (ID) sender{//1. Create URL Nsurl *url = [Nsurl urlwithstring:@ "http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php    "];    2. Create Request object Nsurlrequest *requset = [Nsurlrequest Requestwithurl:url]; 3. Establishing a connection [nsurlconnection sendasynchronousrequest:requset queue:[nsoperationqueue mainqueue] completionhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) {NSLog (@ "%@", data);    }]; NSLog (@ "async");} /*------------------------------POST sync------------------------------------------*/-(ibaction) POST_TB: (ID) sender{//1. Creating a URL Nsurl *url = [Nsurl urlwithstring:@] Http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler    /apinewslist.ashx "];    2. Create Request object Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];    [Request sethttpmethod:@ "POST"]; 3. Add the package body nsstring *str [email protected] "date=20131129&startrecord=5&len=5&udid=1234567890&    terminaltype=iphone&cid=215 ";    Convert string to nsdata format nsdata *databody = [str datausingencoding:nsutf8stringencoding];    4. Set the body [request sethttpbody:databody];    5. Create connection NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];    NSLog (@ "%@", data);    NSLog (@ "Post sync"); }/*------------------------------Post Async------------------------------------------*/-(ibaction) POST_YB: (ID) sender{//1. Create URL Nsurl *url = [Nsurl Urlwithstrin    g:@ "Http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];    2. Create Request object Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];    [Request sethttpmethod:@ "POST"]; 3. Add body NSString *bodystr = @ "to the requesting object date=20131129&startrecord=5&len=5&udid=1234567890&    terminaltype=iphone&cid=215 ";    NSData *databody = [Bodystr datausingencoding:nsutf8stringencoding];    [Request Sethttpbody:databody]; 4. Create the connection [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ (    Nsurlresponse *response, NSData *data, Nserror *connectionerror) {NSLog (@ "%@", data);    }]; NSLog (@ "post async");}


IOS Network parsing

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.