001.
002.
003.
004.
005.
006.
007.
008.
009.
#import "ViewController.h"
010.
011.
@interface
ViewController ()
012.
{
013.
NSMutableArray
*myObject;
014.
015.
016.
NSDictionary
*dict;
017.
018.
019.
NSString
*memberid;
020.
NSString
*name;
021.
NSString
*tel;
022.
023.
UIAlertView
*loading;
024.
}
025.
026.
027.
@end
028.
029.
@implementation
ViewController
030.
031.
@synthesize
receivedData;
032.
033.
- (
void
)viewDidLoad
034.
{
035.
[
super
viewDidLoad];
036.
037.
038.
039.
memberid = @
"MemberID"
;
040.
name = @
"Name"
;
041.
tel = @
"Tel"
;
042.
043.
044.
myObject = [[
NSMutableArray
alloc] init];
045.
046.
}
047.
048.
- (
IBAction
)btnSearch:(
id
)sender {
049.
050.
051.
NSMutableString
*post = [
NSString
stringWithFormat:@
"keyword=%@"
,[txtKeyword text]];
052.
NSData
*postData = [post dataUsingEncoding:
NSASCIIStringEncoding
allowLossyConversion:
YES
];
053.
NSString
*postLength = [
NSString
stringWithFormat:@
"%d"
, [postData length]];
054.
056.
NSMutableURLRequest
*request = [
NSMutableURLRequest
requestWithURL:url
057.
cachePolicy:
NSURLRequestReloadIgnoringLocalCacheData
058.
timeoutInterval:10.0];
059.
[request setHTTPMethod:@
"POST"
];
060.
[request setValue:postLength forHTTPHeaderField:@
"Content-Length"
];
061.
[request setValue:@
"application/x-www-form-urlencoded"
forHTTPHeaderField:@
"Content-Type"
];
062.
[request setHTTPBody:postData];
063.
064.
NSURLConnection
*theConnection=[[
NSURLConnection
alloc] initWithRequest:request delegate:
self
];
065.
066.
067.
[
UIApplication
sharedApplication].networkActivityIndicatorVisible =
YES
;
068.
069.
loading = [[
UIAlertView
alloc] initWithTitle:@
""
message:@
"Please Wait..."
delegate:
nil
cancelButtonTitle:
nil
otherButtonTitles:
nil
];
070.
UIActivityIndicatorView
*progress= [[
UIActivityIndicatorView
alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
071.
progress.activityIndicatorViewStyle =
UIActivityIndicatorViewStyleWhiteLarge
;
072.
[loading addSubview:progress];
073.
[progress startAnimating];
074.
[progress release];
075.
[loading show];
076.
077.
if
(theConnection) {
078.
self
.receivedData =
nil
;
079.
}
else
{
080.
UIAlertView
*connectFailMessage = [[
UIAlertView
alloc] initWithTitle:@
"NSURLConnection "
message:@
"Failed in viewDidLoad"
delegate:
self
cancelButtonTitle:@
"Ok"
otherButtonTitles:
nil
];
081.
[connectFailMessage show];
082.
[connectFailMessage release];
083.
}
084.
085.
[myTable reloadData];
086.
}
087.
088.
- (
void
)connection:(
NSURLConnection
*)connection didReceiveResponse:(
NSURLResponse
*)response
089.
{
090.
receivedData = [[
NSMutableData
alloc] init];
091.
}
092.
093.
- (
void
)connection:(
NSURLConnection
*)connection didReceiveData:(
NSData
*)data
094.
{
095.
sleep(2);
096.
[receivedData appendData:data];
097.
}
098.
099.
- (
void
)connection:(
NSURLConnection
*)connection didFailWithError:(
NSError
*)error
100.
{
101.
102.
[connection release];
103.
[receivedData release];
104.
105.
106.
UIAlertView
*didFailWithErrorMessage = [[
UIAlertView
alloc] initWithTitle: @
"NSURLConnection "
message: @
"didFailWithError"
delegate:
self
cancelButtonTitle: @
"Ok"
otherButtonTitles:
nil
];
107.
[didFailWithErrorMessage show];
108.
[didFailWithErrorMessage release];
109.
110.
111.
NSLog
(@
"Connection failed! Error - %@"
, [error localizedDescription]);
112.
113.
}
114.
115.
- (
void
)connectionDidFinishLoading:(
NSURLConnection
*)connection
116.
{
117.
118.
[
UIApplication
sharedApplication].networkActivityIndicatorVisible =
NO
;
119.
[loading dismissWithClickedButtonIndex:0 animated:
YES
];
120.
121.
122.
[myObject removeAllObjects];
123.
124.
if
(receivedData)
125.
{
126.
127.
128.
129.
130.
id
jsonObjects = [
NSJSONSerialization
JSONObjectWithData:receivedData options:
NSJSONReadingMutableContainers
error:
nil
];
131.
132.
133.
for
(
NSDictionary
*dataDict in jsonObjects) {
134.
NSString
*strMemberID = [dataDict objectForKey:@
"MemberID"
];
135.
NSString
*strName = [dataDict objectForKey:@
"Name"
];
136.
NSString
*strTel = [dataDict objectForKey:@
"Tel"
];
137.
138.
dict = [
NSDictionary
dictionaryWithObjectsAndKeys:
139.
strMemberID, memberid,
140.
strName, name,
141.
strTel, tel,
142.
nil
];
143.
[myObject addObject:dict];
144.
}
145.
146.
[myTable reloadData];
147.
}
148.
149.
150.
[connection release];
151.
[receivedData release];
152.
}
153.
154.
- (
NSInteger
)tableView:(
UITableView
*)tableView numberOfRowsInSection:(
NSInteger
)section
155.
{
156.
157.
int
nbCount = [myObject count];
158.
if
(nbCount == 0)
159.
return
1;
160.
else
161.
return
[myObject count];
162.
163.
}
164.
165.
- (
UITableViewCell
*)tableView:(
UITableView
*)tableView cellForRowAtIndexPath:(
NSIndexPath
*)indexPath
166.
{
167.
168.
static
NSString
*CellIdentifier = @
"Cell"
;
169.
170.
UITableViewCell
*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
171.
if
(cell ==
nil
) {
172.
173.
cell = [[[
UITableViewCell
alloc] initWithStyle :
UITableViewCellStyleSubtitle
174.
reuseIdentifier : CellIdentifier] autorelease];
175.
}
176.
177.
int
nbCount = [myObject count];
178.
if
(nbCount ==0)
179.
cell.textLabel.text = @
""
;
180.
else
181.
{
182.
NSDictionary
*tmpDict = [myObject objectAtIndex:indexPath.row];
183.
184.
185.
NSMutableString
*text;
186.
text = [
NSString
stringWithFormat:@
"MemberID : %@"
,[tmpDict objectForKey:memberid]];
187.
188.
189.
NSMutableString
*detail;
190.
detail = [
NSString
stringWithFormat:@
"Name : %@ , Tel : %@"
191.
,[tmpDict objectForKey:name]
192.
,[tmpDict objectForKey:tel]];
193.
194.
cell.textLabel.text = text;
195.
cell.detailTextLabel.text= detail;
196.
197.
198.
199.
200.
}
201.
return
cell;
202.
}
203.
204.
- (
void
)didReceiveMemoryWarning
205.
{
206.
[
super
didReceiveMemoryWarning];
207.
208.
}
209.
210.
- (
void
)dealloc {
211.
[txtKeyword release];
212.
[myTable release];
213.
[
super
dealloc];
214.
}
215.
216.
@end