Hi ,
I am new to iPhone development and having problems with NSURLConnection while calling my .NET Based Webservice using HTTP Post method. (I m using iOS 5 & ARC)
My problem is sometime my connection is established with my service and I get the reponse back in JSON and sometimes it doesn't and I get error 'The Network Connection was Lost'
Is this behavior due to buggy code ? Or NSUrlCOnnection class ?
I have seen some posts in this forum which suggests to use ASIHTTPRequest library... but I am using NSUrlConnection as I just have to send HTTP Post request and recive response nothing fancy ...
Is it also possible that this is happening in simulator and on real device it will work fine ? If I open the same WebService URL in Safari of my simulator it works fine...
Here is my code:
Code:
(IBAction)btnCallService:(id)sender {
[XYZActivity startAnimating];
// So I could show activity on my main UI thread.
[self performSelector: @selector(CallXYZSService)
withObject: nil
afterDelay: 0];
}
(void) CallXYZSService
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *jsonRequest_NE = [NSString stringWithFormat:@"ID=%@&Password=%@",strID,strPwd];
NSString *jsonRequest = [jsonRequest_NE stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Request: %@", jsonRequest);
NSString *myURL = @"http://localwinhost/JSON_Service.asmx/GetFunction1";
NSString *fixedURL = [myURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:fixedURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:1 timeoutInterval:30];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"close" forHTTPHeaderField:@"Connection"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
//[req setHTTPBody: [postStr dataUsingEncoding:NSUTF8StringEncoding]];
loginConn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (loginConn) {
webData = [NSMutableData data];
}
else
{
NSLog(@"Connection Failed");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result"
message:@"Could not establish Connection"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
-(void) connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *) response{
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *)connection
didReceiveData:(NSData *) data {
[webData appendData:data];
}
-(void) connection:(NSURLConnection *)connection
didFailWithError:(NSError *) error {
[loginActivity stopAnimating];
txtLoginError.text=[error description];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result"
message:[error description]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(@"DONE. Received Bytes: %d", [webData length]);
//Work with Response
}
Thanks in advance for your help.
Maverick