xml 加载后 xml 响应

发布于 2024-12-28 15:05:51 字数 2115 浏览 1 评论 0 原文

我正在尝试使用 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>

I'm trying to hit a REST api to see if a user is valid. I know the connectionDidFinishLoading runs, but I'm not sure how to check the response (xml). Please advise.

The signIn function gets the ball rolling

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2025-01-04 15:05:51

通常要解析 XML,您会使用 NSXMLParser,但字符串响应简单如“valid login"

如果是这种情况,您只需查找包含字符串 @"valid login" 但不包含 @"invalid login" 的响应

if (([responseText rangeOfString:@"invalid login"].location == NSNotFound) && ([responseText rangeOfString:@"valid login"].location != NSNotFound)){
    // Congrats valid
}

如果(更好)成功响应为“successful login”,则 if 语句变得更容易遵循。

if ([responseText rangeOfString:@"successful login"].location != NSNotFound){
    // Congrats valid
}

Ordinarily to parse XML you would use NSXMLParser, 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 (([responseText rangeOfString:@"invalid login"].location == NSNotFound) && ([responseText rangeOfString:@"valid login"].location != NSNotFound)){
    // Congrats valid
}

If (even better) a successful response would be "<string xmlns="http://thedomain.com/">successful login</string>" then the if statement becomes easier to follow.

if ([responseText rangeOfString:@"successful login"].location != NSNotFound){
    // Congrats valid
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文