IOS _ network request_proxy Method

Source: Internet
Author: User

IOS _ network request_proxy Method

# Pragma mark-network request proxy method (asynchronous)-(IBAction) DelegateButtonDidClicked :( UIButton *) sender {// 1. Splice urlString. The URL must contain http: // NSString * urlString = @ http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx? Date = 20131129 & startRecord = 1 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 213; // 2. Create a URL Based on the string (Uniform Resource Locator) NSURL * url = [NSURL URLWithString: urlString]; // 3. Create a request object NSURLRequest * request = [NSURLRequest requestWithURL: url] based on the url. // 4. Request data (proxy callback method) follows the proxy
  
   
[NSURLConnection connectionWithRequest: request delegate: self];}
  

Proxy Method
The client receives a response from the server.

# Pragma mark | the client receives the response from the server-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {// only execute once // initialize the receiveData object self. receiveData = [NSMutableData data];}

Data received by the client (not all at once)

# Pragma mark | data received by the client (not all obtained at a time)-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {// new data is returned each time. // concatenate data (requesting a bit of stitching) [self. receiveData appendData: data];}

The client has received the data.

# Pragma mark | the client has received the data-(void) connectionDidFinishLoading :( NSURLConnection *) connection {// parse the data NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: self. receiveData options :( NSJSONReadingMutableContainers) error: nil]; NSArray * array = dict [@ news]; self. newsArray = [NSMutableArray array]; for (NSDictionary * smallDict in array) {NewsModal * modal = [[NewsModal alloc] init]; [modal convert: smallDict]; [self. newsArray addObject: modal];} for (NewsModal * modal in self. newsArray) {NSLog (% @, modal. title );}}

Network request failed

# Pragma mark | Network request failed-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (, error );}

 

Related Article

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.