Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-06-2012, 04:19 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 2
mavericki is on a distinguished road
Question Issues calling WebService using NSURLConnection-Inconsistant connection behavior

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
mavericki is offline   Reply With Quote
Old 02-07-2012, 01:27 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 2
mavericki is on a distinguished road
Default

Any help plz....
mavericki is offline   Reply With Quote
Reply

Bookmarks

Tags
nsurlconnection, webservice

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
18 members and 384 guests
Alex-alex, Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, LunarMoon, MAMN84, n00b, pbart, reficul, Retouchable, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:15 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0