试试看....
Set Delegate:
<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
Declare :
NSMutableData *responceData;
NSURLConnection *connect;
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
responceData = [[NSMutableData alloc]init];
NSString *post = [NSString stringWithFormat:@"op=post_comment&business_id=%@&name=%@&email=%@&comment=%@",busId, name.text,email.text,myTextView.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.dignizant.com/red_pages/web_services/index.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
connect = [NSURLConnection connectionWithRequest:request delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[responceData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR : %@" , error);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:[NSString stringWithFormat:@"%@",error] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//initialize convert the received data to string with UTF8 encoding
NSString *responceString = [[NSString alloc] initWithData:responceData
encoding:NSUTF8StringEncoding];
NSLog(@"%@" , responceString);
NSMutableDictionary *message = [responceString JSONValue];
[message valueForKey:@"message"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:[message valueForKey:@"message"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
希望我有所帮助