xml 加载后 xml 响应
我正在尝试使用 REST api 来查看用户是否有效。我知道 connectionDidFinishLoading
运行,但我不确定如何检查响应(xml)。请指教。
signIn
函数让一切顺利进行
- (IBAction)signIn:(id)sender;
{
[emailError setHidden:YES];
if([emailAddressTxt text] && [passwordTxt text]) {
// send user/pass to server for validation
if([self NSStringIsValidEmail:[emailAddressTxt text]]) {
NSString *post = [NSString stringWithFormat:@"Email=%@&Password=%@", emailAddressTxt.text, passwordTxt.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.mySite.com/validate.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:request delegate:self];
}
} else {
// give error dialogue
[emailError setText:@"User not found"];
[emailError setHidden:NO];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//[signInData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
//[signInData appendData:d];
// updated to:
signInData = (NSMutableData *)d;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Fail..
[emailError setText:@"Connection Error"];
[emailError setHidden:NO];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseText = [[NSString alloc] initWithData:signInData encoding:NSUTF8StringEncoding];
NSLog(@"%@", @"check");
NSLog(@"%@", responseText);
}
// an example response would be:
// <string xmlns="http://thedomain.com/">invalid login</string>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常要解析
XML
,您会使用NSXMLParser
,但字符串响应简单如“valid login
"如果是这种情况,您只需查找包含字符串
@"valid login"
但不包含@"invalid login" 的响应
如果(更好)成功响应为“
successful login
”,则 if 语句变得更容易遵循。Ordinarily to parse
XML
you would useNSXMLParser
, but with a string response a simple as "<string xmlns="http://thedomain.com/">invalid login</string>
" I'm assuming that a valid login would look like this :"<string xmlns="http://thedomain.com/">valid login</string>
"If that is the case you can simply look for a response which contains the string
@"valid login"
but does not contain@"invalid login"
If (even better) a successful response would be "
<string xmlns="http://thedomain.com/">successful login</string>
" then the if statement becomes easier to follow.