4
我从我的iPhone应用程序的PHP Web服务连接,我即做一个简单的事情 1.获取用户输入的:在文本字段从 用户名密码 iphone表单并将其发送到PHP Post请求Web服务。在Web服务端我收到无非就是插入MySQL数据库的空白领域等.......PHP的Web服务不是从iPhone应用程序中获取数据
样品Web服务的代码是:
**********************SAMPLE CODE FOR WEB SERVICES**************
<?php
$con=mysql_connect("localhost","root","123456") or die(mysql_error());
mysql_select_db("eventsfast",$con);
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
echo $username;
echo $password;
$data = $_REQUEST;
$fp = fopen("log.txt", "w");
fwrite($fp, $data['username']);
fwrite($fp, $data['password']);
$sql="INSERT INTO users(username,password) VALUES('{$username}','{$password}')";
if(!mysql_query($sql,$con))
{
die('Error:'.mysql_error());
}
echo json_encode("1 record added to users table");
mysql_close($con);
echo "test";
?>
******************************PHP***************** **************
*****************************IPHONE EVENT CODE******************
#import "postdatawithphpViewController.h"
@implementation postdatawithphpViewController
@synthesize userName,password;
-(IBAction) postdataid) sender
{
NSLog(userName.text);
NSLog(password.text);
NSString * dataTOB=[userName.text stringByAppendingString:password.text];
NSLog(dataTOB);
NSData * postData=[dataTOB dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(postLength);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8888/write.php"]];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(error==nil)
NSLog(@"Error is nil");
else
NSLog(@"Error is not nil");
NSLog(@"success!");
}
************************************************** **************
请帮助... ..........
我认为这是重复的问题。注意这一点。 http://stackoverflow.com/questions/2643247/unable-to-get-data-from-iphone-application – 2010-04-15 07:10:41