#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