【问题标题】:xml post loading xml responsexml 后加载 xml 响应
【发布时间】:2012-01-24 17:53:56
【问题描述】:

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

【问题讨论】:

  • 你是在问如何解析xml响应?

标签: ios nsurlrequest nsmutableurlrequest


【解决方案1】:

通常要解析XML,您将使用NSXMLParser,但字符串响应很简单,例如“&lt;string xmlns="http://thedomain.com/"&gt;invalid login&lt;/string&gt;”,我假设有效的登录名如下所示:“&lt;string xmlns="http://thedomain.com/"&gt;valid login&lt;/string&gt;

如果是这种情况,您可以简单地查找包含字符串 @"valid login" 但不包含 @"invalid login" 的响应

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

如果(甚至更好)成功的响应是“&lt;string xmlns="http://thedomain.com/"&gt;successful login&lt;/string&gt;”,那么 if 语句就更容易理解了。

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

【讨论】:

  • 我完全改变了加载 XML 的方式,但这个解决方案适用于我的“简单”解析。
猜你喜欢
  • 1970-01-01
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多