2012-02-06 47 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发送异步请求 - 答案就在那里。

相关问题