2012-01-26 92 views
0


我想要做的是,我正在創建一個非常簡單的iphone應用程序,textfield和一個按鈕,當它按下時它應該將文本字段中的文本發送到本地託管的servlet玻璃魚3.1.1)和公正,以確保該請求被做我輸入一個簡單的

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

每一次我打的按鈕,我去檢查服務器日誌,它空! 我在瀏覽器中嘗試了相同的URL,然後檢查日誌,「收到的請求」在日誌中。通過iphone發送文本到servlet

連接到按鈕的方法是:
-(IBAction) connectToServer:(id)sender{ NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/Test/Home"]]; NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; }

我真的不知道,如果我在這裏失去任何東西,任何幫助?

+0

也許這樣? http://stackoverflow.com/questions/5580803/sending-data-to-a-server-from-ipad/5581119#5581119 – basvk

回答

0

你還沒有開始連接

所以,你的行動的方法應該是這樣的:

- (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]; 
} 

或者,您可以使用一個不同的初始化:

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

或者

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

如果你不使用ARC,不要忘記內存管理:) – Ievgen

+0

什麼都沒做沒有工作... – 4kr4m

+0

你是否實現了NSURLConnection委託的方法? – Ievgen