2012-02-06 46 views
4

我試圖從我的應用程序發送一個建議到我的Web服務器上的一個PHP文件,我已經在我的瀏覽器中測試了php腳本,它向用戶發送一封電子郵件並將建議存儲在我的數據庫中,這一切都正常。 。並運行下面的腳本,我得到通過IOS連接成功,但是我沒有收到我的數據庫中的結果..使用IOS 5創建HTTP GET請求的最簡單方法是什麼?

NSString *post = [NSString stringWithFormat:@"http://blahblah.com/suggest.php?s=%@&n=%@&e=%@", suggestion, name, email]; 

    // Create the request. 
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:post] 
               cachePolicy:NSURLRequestUseProtocolCachePolicy 
              timeoutInterval:60.0]; 

    // create the connection with the request 
    // and start loading the data 
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    if (theConnection) { 

     NSLog(@"Connection establisted successfully"); 

    } else { 

     NSLog(@"Connection failed."); 

    } 

我已經檢查了所有的字符串時,和編碼爲%20等所有空間。可任何人都會看到明顯的原因,爲什麼我的腳本不能工作?

什麼是最簡單的方式來從我的應用程序發出的HTTP請求,而無需打開Safari瀏覽器?

回答

7

你的問題是你正在創建連接,但沒有發送實際的「連接」請求。而不是使用這段代碼

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

嘗試:

NSURLResponse* response = nil; 
NSData* data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil] 

這是快速和骯髒的解決方案,但要記住的是,雖然這個連接過程中,你的UI線程就好像是被凍結。解決這個問題的方法是使用異步連接方法,這比上面更復雜一些。搜索網絡NSURLConnection發送異步請求 - 答案就在那裏。

相關問題