001.
002.
003.
004.
005.
006.
007.
008.
009.
#import "DetailViewController.h"
010.
011.
#import "ViewController.h"
012.
013.
@interface
DetailViewController ()
014.
{
015.
NSString
*actionCommand;
016.
}
017.
018.
@end
019.
020.
@implementation
DetailViewController
021.
022.
@synthesize
receivedData;
023.
024.
- (
id
)initWithNibName:(
NSString
*)nibNameOrNil bundle:(
NSBundle
*)nibBundleOrNil
025.
{
026.
self
= [
super
initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
027.
if
(
self
) {
028.
029.
}
030.
return
self
;
031.
}
032.
033.
- (
void
)viewDidLoad
034.
{
035.
[
super
viewDidLoad];
036.
037.
038.
actionCommand = @
"LOAD"
;
039.
040.
if
([actionCommand isEqualToString:@
"LOAD"
] ){
041.
042.
NSMutableString
*post = [
NSString
stringWithFormat:@
"sMemberID=%@"
,[
self
.sMemberID description]];
043.
044.
NSData
*postData = [post dataUsingEncoding:
NSASCIIStringEncoding
allowLossyConversion:
YES
];
045.
NSString
*postLength = [
NSString
stringWithFormat:@
"%d"
, [postData length]];
046.
048.
NSMutableURLRequest
*request = [
NSMutableURLRequest
requestWithURL:url
049.
cachePolicy:
NSURLRequestReloadIgnoringLocalCacheData
050.
timeoutInterval:10.0];
051.
[request setHTTPMethod:@
"POST"
];
052.
[request setValue:postLength forHTTPHeaderField:@
"Content-Length"
];
053.
[request setValue:@
"application/x-www-form-urlencoded"
forHTTPHeaderField:@
"Content-Type"
];
054.
[request setHTTPBody:postData];
055.
056.
NSURLConnection
*theConnection=[[
NSURLConnection
alloc] initWithRequest:request delegate:
self
];
057.
058.
059.
060.
if
(theConnection) {
061.
self
.receivedData =
nil
;
062.
}
else
{
063.
UIAlertView
*connectFailMessage = [[
UIAlertView
alloc] initWithTitle:@
"NSURLConnection "
message:@
"Failed in viewDidLoad"
delegate:
self
cancelButtonTitle:@
"Ok"
otherButtonTitles:
nil
];
064.
[connectFailMessage show];
065.
[connectFailMessage release];
066.
}
067.
068.
}
069.
}
070.
071.
072.
- (
void
)connection:(
NSURLConnection
*)connection didReceiveResponse:(
NSURLResponse
*)response
073.
{
074.
receivedData = [[
NSMutableData
alloc] init];
075.
}
076.
077.
- (
void
)connection:(
NSURLConnection
*)connection didReceiveData:(
NSData
*)data
078.
{
079.
[receivedData appendData:data];
080.
}
081.
082.
- (
void
)connection:(
NSURLConnection
*)connection didFailWithError:(
NSError
*)error
083.
{
084.
085.
[connection release];
086.
[receivedData release];
087.
088.
089.
UIAlertView
*didFailWithErrorMessage = [[
UIAlertView
alloc] initWithTitle: @
"NSURLConnection "
message: @
"didFailWithError"
delegate:
self
cancelButtonTitle: @
"Ok"
otherButtonTitles:
nil
];
090.
[didFailWithErrorMessage show];
091.
[didFailWithErrorMessage release];
092.
093.
094.
NSLog
(@
"Connection failed! Error - %@"
, [error localizedDescription]);
095.
096.
}
097.
098.
- (
void
)connectionDidFinishLoading:(
NSURLConnection
*)connection
099.
{
100.
101.
102.
if
(receivedData)
103.
{
104.
105.
106.
107.
108.
109.
110.
if
([actionCommand isEqualToString:@
"LOAD"
] ){
111.
id
jsonObjects = [
NSJSONSerialization
JSONObjectWithData:receivedData options:
NSJSONReadingMutableContainers
error:
nil
];
112.
113.
114.
NSString
*strMemberID = [jsonObjects objectForKey:@
"MemberID"
];
115.
NSString
*strName = [jsonObjects objectForKey:@
"Name"
];
116.
NSString
*strTel = [jsonObjects objectForKey:@
"Tel"
];
117.
NSLog
(@
"MemberID = %@"
,strMemberID);
118.
NSLog
(@
"Name = %@"
,strName);
119.
NSLog
(@
"Tel = %@"
,strTel);
120.
121.
lblMemberID.text = strMemberID;
122.
txtName.text = strName;
123.
txtTel.text = strTel;
124.
}
125.
126.
127.
if
([actionCommand isEqualToString:@
"SAVE"
] ){
128.
129.
130.
131.
132.
133.
id
jsonObjects = [
NSJSONSerialization
JSONObjectWithData:receivedData options:
NSJSONReadingMutableContainers
error:
nil
];
134.
135.
136.
NSString
*strStatus = [jsonObjects objectForKey:@
"Status"
];
137.
NSString
*strMessage = [jsonObjects objectForKey:@
"Message"
];
138.
NSLog
(@
"Status = %@"
,strStatus);
139.
NSLog
(@
"Message = %@"
,strMessage);
140.
141.
142.
if
( [strStatus isEqualToString:@
"1"
] ){
143.
UIAlertView
*completed =[[
UIAlertView
alloc]
144.
initWithTitle:@
": -) Completed!"
145.
message:strMessage delegate:
self
146.
cancelButtonTitle:@
"OK"
otherButtonTitles:
nil
];
147.
[completed show];
148.
}
149.
else
150.
{
151.
UIAlertView
*error =[[
UIAlertView
alloc]
152.
initWithTitle:@
": ( Error!"
153.
message:strMessage delegate:
self
154.
cancelButtonTitle:@
"OK"
otherButtonTitles:
nil
];
155.
[error show];
156.
}
157.
158.
}
159.
160.
}
161.
162.
163.
[connection release];
164.
[receivedData release];
165.
}
166.
167.
168.
169.
170.
- (
IBAction
)btnSave:(
id
)sender {
171.
172.
actionCommand = @
"SAVE"
;
173.
174.
175.
NSMutableString
*post = [
NSString
stringWithFormat:@
"sMemberID=%@&sName=%@&sTel=%@"
,
176.
[
self
.sMemberID description],[txtName text],[txtTel text]];
177.
178.
NSData
*postData = [post dataUsingEncoding:
NSASCIIStringEncoding
allowLossyConversion:
YES
];
179.
NSString
*postLength = [
NSString
stringWithFormat:@
"%d"
, [postData length]];
180.
182.
NSMutableURLRequest
*request = [
NSMutableURLRequest
requestWithURL:url
183.
cachePolicy:
NSURLRequestReloadIgnoringLocalCacheData
184.
timeoutInterval:10.0];
185.
[request setHTTPMethod:@
"POST"
];
186.
[request setValue:postLength forHTTPHeaderField:@
"Content-Length"
];
187.
[request setValue:@
"application/x-www-form-urlencoded"
forHTTPHeaderField:@
"Content-Type"
];
188.
[request setHTTPBody:postData];
189.
190.
NSURLConnection
*theConnection=[[
NSURLConnection
alloc] initWithRequest:request delegate:
self
];
191.
192.
193.
194.
if
(theConnection) {
195.
self
.receivedData =
nil
;
196.
}
else
{
197.
UIAlertView
*connectFailMessage = [[
UIAlertView
alloc] initWithTitle:@
"NSURLConnection "
message:@
"Failed in viewDidLoad"
delegate:
self
cancelButtonTitle:@
"Ok"
otherButtonTitles:
nil
];
198.
[connectFailMessage show];
199.
[connectFailMessage release];
200.
}
201.
202.
}
203.
204.
205.
- (
IBAction
)btnCancel:(
id
)sender {
206.
207.
ViewController *main = [[[ViewController alloc] initWithNibName:
nil
bundle:
nil
] autorelease];
208.
209.
[
self
presentViewController:main animated:
YES
completion:
NULL
];
210.
211.
}
212.
213.
- (
void
)didReceiveMemoryWarning
214.
{
215.
[
super
didReceiveMemoryWarning];
216.
217.
}
218.
219.
- (
void
)dealloc {
220.
[txtName release];
221.
[txtTel release];
222.
[lblMemberID release];
223.
[txtName release];
224.
[
super
dealloc];
225.
}
226.
227.
@end