IOS network resolution
Synchronous and asynchronous network resolution
/* ------------------------ Get synchronization -------------------------------------*/
-(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 (@ "Synchronize ");
}
/* --------------------------- Get asynchronous ------------------------------------*/
-(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 (@ "Asynchronous ");
}
/* ------------------------------ Post synchronization ------------------------------------------*/
-(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 a package
NSString * str = @ "date = 20131129 & startRecord = 5 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 215 ";
// Convert the 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 synchronization ");
}
/* ------------------------------ Post asynchronous ------------------------------------------*/
-(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 the body to the 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 asynchronous ");
}
/* ------------------------ Get synchronization --------------------------------------- */-(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 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 (@ "synchronous");}/* --------------------------- 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 a request object NSURLRequest * requset = [NSURLRequest requestWithURL: url]; // 3. establish connection [NSURLConnection sendAsynchronousRequest: requset queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {NSLog (@ "% @", data) ;}]; NSLog (@ "Asynchronous") ;}/ * ------------------------------ post synchronous handler */-(IBAction) POST_TB :( id) sender {// 1. create 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 NSString * str = @ "date = 20131129 & startRecord = 5 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 215 "; // convert the string to NSDATA format NSData * dataBody = [str dataUsingEncoding: NSUTF8StringEncoding]; // 4. set the request body [request setHTTPBody: dataBody]; // 5. create a connection NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; NSLog (@ "% @", data); NSLog (@ "POST synchronization ");} /* ------------------------------ post asynchronous -------------------------------------------- */-(IBAction) POST_YB :( id) sender {// 1. create 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 NSString * bodyStr = @ "date = 20131129 & startRecord = 5 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 215" to the request object "; NSData * dataBody = [bodyStr dataUsingEncoding: NSUTF8StringEncoding]; [request setHTTPBody: dataBody]; // 4. create connection [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {NSLog (@ "% @", data) ;}]; NSLog (@ "POST asynchronous ");}