我试图从我的应用程序发送一个建议到我的网络服务器上的PHP文件,我已经在我的浏览器中测试了php脚本,它向用户发送了一封电子邮件,并将建议存储在我的数据库中,这一切都正常.当运行以下脚本时,我通过IOS成功连接但是我没有在我的数据库中收到结果..
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.");
}
Run Code Online (Sandbox Code Playgroud)
我检查了所有的字符串并使用%20等编码了所有空格.任何人都可以看到为什么我的脚本不起作用的任何明显的原因?
在不打开Safari的情况下,从我的应用程序发出HTTP请求的最简单方法是什么?
您的问题是您正在创建连接,但不发送实际的"连接"请求.代替
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
Run Code Online (Sandbox Code Playgroud)
尝试使用这段代码:
NSURLResponse* response = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil]
Run Code Online (Sandbox Code Playgroud)
这是一个快速而又脏的解决方案,但请记住,当此连接正在进行时,您的UI线程似乎将被冻结.解决它的方法是使用异步连接方法,这比上面的方法复杂一点.在网上搜索NSURLConnection发送异步请求 - 答案就在那里.
归档时间: |
|
查看次数: |
4685 次 |
最近记录: |