Search code examples
iphoneservletshttpconnection

send text to servlet via iphone



what i am trying to do is, i am creating a very simple iphone app, textfield and a button that when pressed it should send the text in the text field to a servlet hosted locally (glass fish 3.1.1) and just to make sure the request is done i type a simple

system.out.println("Request received");

and each time i hit the button and i go to check the server log, it empty ! i tried the same url in the browser then check the log,"Request received" is in the log.

the method connected to the button is:
-(IBAction) connectToServer:(id)sender{ NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/Test/Home"]]; NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; }

i really dont know if i am missing any thing here, any help ??


Solution

  • You haven't started the connection

    So, your action's method should look like:

    - (IBAction)connectToServer:(id)sender {
        NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/Test/Home"]];
        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
        [theConnection start];
    }
    

    Or, you may use a different initializer:

    - (IBAction)connectToServer:(id)sender {
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/Test/Home"]];
        [NSURLConnection connectionWithRequest:theRequest delegate:self];
    }
    

    Or

    - (IBAction)connectToServer:(id)sender {
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/Test/Home"]];
        [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
    }