如何从服务器获取数据?

时间:2011-10-21 11:26:50

标签: iphone objective-c ios xcode xcode4

我正在创建一个用户可以登录的登录应用程序。

如果用户已经注册,那么他应该能够使用保存在服务器上的相同数据登录,并且应该能够获得成功的警报视图,如果不是错误警报。

不幸的是,我无法做到这一点。

请帮帮我。

这是我的代码:

-(IBAction)btn_login: (id) sender
{

NSString *firstname = Firstname.text;

NSString *lastname = Lastname.text;

NSString *post = [NSString stringWithFormat:@"fname=%@&lname=%@",firstname,lastname];

NSString *hostStr =@"http://www.yoursite.com/iphone.php";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];

NSString *serverOutput = [[NSString alloc]initWithData:dataURL encoding:NSASCIIStringEncoding];

if ([serverOutput isEqualToString:@"Yes"])
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Congrats" message:@"You are authorised" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
    [alert show];
    [alert release];

}
else {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Invalid Username and password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
    [alert show];
    [alert release];
}

Firstname.text=nil;
Lastname.text=nil;

}

感谢

1 个答案:

答案 0 :(得分:3)

你需要提供一个“?”添加参数后的地址之后。

更改

NSString *hostStr=@"http://www.yoursite.com/iphone.php";

NSString *hostStr=@"http://www.yoursite.com/iphone.php?";

PS:NSData dataWithContentsOfUrl是一种可怕的方法。使用NSURLConnection并对基本身份验证使用身份验证质询。阅读URL Loading System Programming Guide