IOS. Accessing the Web Service. Asynchronous GET Request method

Source: Internet
Author: User

#import<UIKit/UIKit.h>#import "t20140628024750nsnumber+message.h"#import "t20140628024750nsstring+urlencoding.h"@interfaceT20140628024750viewcontroller:uitableviewcontroller<nsurlconnectiondelegate>@property (nonatomic,strong) Nsmutablearray*ListData;//receives data returned from the server. @property (strong,nonatomic) Nsmutabledata *datas;//Query All-(void) FindAll;@end
#import "T20140628024750ViewController.h"@interfaceT20140628024750viewcontroller ()@end@implementationT20140628024750viewcontroller- (void) viewdidload{[Super Viewdidload]; //1. Initialize Data[self findAll];}- (void) didreceivememorywarning{[Super didreceivememorywarning];}#pragmaMark Table datasource-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{returnSelf.listData.count;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//1. Initialize cell reuse    StaticNSString *reusecell =@"Reusecell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:reusecell]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:reusecell]; }        //2. Configuration Reuse Cell Datansmutabledictionary* dict =Self.listdata[indexpath.row]; Cell.textLabel.text= [Dict Objectforkey:@"Content"]; Cell.detailTextLabel.text= [Dict Objectforkey:@"CDate"]; returncell;}-(void) findall{NSString*strurl = [[NSString alloc] Initwithformat:@"http://127.0.0.1:8080/kujizu/test01.html"]; Nsurl*url =[Nsurl Urlwithstring:[strurl urlencodedstring]; Nsurlrequest*request =[[Nsurlrequest alloc] initwithurl:url]; Nsurlconnection*connection = [[Nsurlconnection alloc] Initwithrequest:requestDelegate: Self]; if(connection) {_datas= [NsmutabledataNew]; }}#pragmaMark-nsurlconnection callback Method-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data {[_datas appenddata:data];}-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) Error {NSLog (@"%@", [Error localizeddescription]);}- (void) Connectiondidfinishloading: (nsurlconnection*) connection {NSLog (@"Request Complete ..."); Nsdictionary* Resdict =[nsjsonserialization jsonobjectwithdata:_datas options:nsjsonreadingallowfragments Error:nil]; if(Resdict = =Nil) {Self.listdata=[[Nsmutablearray alloc] init]; Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"error Message"Message@"no data. " Delegate: Nil Cancelbuttontitle:@"OK"Otherbuttontitles:nil];    [Alertview show]; }Else{NSNumber*resultcodeobj = [Resdict objectforkey:@"ResultCode"]; if([Resultcodeobj IntegerValue] >=0) {Self.listdata= [Resdict Objectforkey:@"Record"];        [Self.tableview Reloaddata]; } Else{nsstring*errorstr =[Resultcodeobj errormessage]; Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"error Message"Message:errorstrDelegate: Nil Cancelbuttontitle:@"OK"Otherbuttontitles:nil];        [Alertview show]; }    }    }@end

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.