【发布时间】:2012-06-10 04:50:50
【问题描述】:
您好,我想知道如何使用 NSURLConnection 或 JSon 将数据从 4 个 UITextField 发送到网站上的 PHP 脚本。哪个更容易,我该怎么做呢?
提前致谢!
【问题讨论】:
-
您不确定其中的哪一部分?从文本字段中提取数据还是将数据发送到服务器?
标签: iphone sdk uitextfield
您好,我想知道如何使用 NSURLConnection 或 JSon 将数据从 4 个 UITextField 发送到网站上的 PHP 脚本。哪个更容易,我该怎么做呢?
提前致谢!
【问题讨论】:
标签: iphone sdk uitextfield
最简单的方式(同步)
NSString strForTextField1 = textField1.text;
NSString strForTextField2 = textField1.text;
NSString strForTextField3 = textField1.text;
NSString strForTextField4 = textField1.text;
//Build the request
NSString *stringOfRequest = [NSString stringWithFormat:@"www.yourserver.com?var1=%@&var2=%@&var3=%@&var4=%@", strForTextField1, strForTextField2, strForTextField3, strForTextField4];
NSURL *url = [NSURL URLWithString:stringOfRequest];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response;
NSError *error;
//send it synchronous
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(!error)
{
//log response
NSLog(@"Response from server = %@", responseString);
}
【讨论】: