解析 HTML 响应 - iPhone 应用程序

发布于 2024-10-06 06:31:41 字数 1678 浏览 4 评论 0原文

我正在创建一个应用程序。我使用 HTTP POST 方法发送登录信息,并且从服务器收到的回复是 HTML 格式。如何解析该 HTML 并添加不同的成功或失败方法?我想要实现的是,登录失败时,它应该使用 UIAlerView 显示消息,成功登录后,应用程序应该用动画更改视图。 :)

我现在使用的代码:

- (IBAction) loginButton: (id) sender {
indicator.hidden = NO;
[indicator startAnimating];
loginbutton.enabled = NO;

// Create the username and password string.
// username and password are the username and password to login with
NSString *postString = [[NSString alloc] initWithFormat:@"username=%@&password=%@",userName, password];
// Package the string in an NSData object
NSData *requestData = [postString dataUsingEncoding:NSASCIIStringEncoding];

// Create the URL request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://localhost/dologin.php"]];  // create the URL request
[request setHTTPMethod: @"POST"];   // you're sending POST data
[request setHTTPBody: requestData];  // apply the post data to be sent

// Call the URL
NSURLResponse *response;  // holds the response from the server
NSError *error;   // holds any errors
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error];  // call the URL

/* If the response from the server is a web page, dataReturned will hold the string of the HTML returned. */
NSString *dataReturned = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];

alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:[NSString stringWithFormat:@"%@",dataReturned] delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alertWithOkButton show];
[alertWithOkButton release];
}

I'm creating an app. I'm sending the login info using HTTP POST method and the replyI'm getting from server is in HTML format. How can I parse that HTML and add different methods for succession or failure? What I'm trying to achieve is, upon login failure it should show the message using UIAlerView and upon successful login, the app should change the view with animation. :)

The code I'm using right now:

- (IBAction) loginButton: (id) sender {
indicator.hidden = NO;
[indicator startAnimating];
loginbutton.enabled = NO;

// Create the username and password string.
// username and password are the username and password to login with
NSString *postString = [[NSString alloc] initWithFormat:@"username=%@&password=%@",userName, password];
// Package the string in an NSData object
NSData *requestData = [postString dataUsingEncoding:NSASCIIStringEncoding];

// Create the URL request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://localhost/dologin.php"]];  // create the URL request
[request setHTTPMethod: @"POST"];   // you're sending POST data
[request setHTTPBody: requestData];  // apply the post data to be sent

// Call the URL
NSURLResponse *response;  // holds the response from the server
NSError *error;   // holds any errors
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error];  // call the URL

/* If the response from the server is a web page, dataReturned will hold the string of the HTML returned. */
NSString *dataReturned = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];

alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:[NSString stringWithFormat:@"%@",dataReturned] delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alertWithOkButton show];
[alertWithOkButton release];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你げ笑在眉眼 2024-10-13 06:31:41

我所做的就是使用 HTMLparser 类。如果您收到 HTML 格式的响应,该类非常有用。

What I did exactly is I used HTMLparser class. This class is very useful if you're getting response in HTML format.

二智少女 2024-10-13 06:31:41
-(void)startParsingForLogin:(NSString *)userIdStr Password:(NSString *)passwordStr 
{

NSString *urlString = [NSString stringWithFormat:@"http://www.example.com/loginxml.php?username=%@&password=%@",userIdStr,passwordStr];
////////NSLog(@"urlString : %@",urlString);
NSURL *xmlURL = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:xmlURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]autorelease];

NSURLResponse *returnedResponse = nil;
NSError *returnedError = nil;
NSData *itemData  = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnedResponse error:&returnedError];
//NSString *itemString = [[[NSString alloc] initWithBytes:[itemData bytes] length:[itemData length] encoding:NSUTF8StringEncoding]autorelease];

//////NSLog(@"itemString : %@",itemString);


xmlParser = [[NSXMLParser alloc] initWithData:itemData];        
[xmlParser setDelegate:self];

[xmlParser parse];

}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
////////NSLog(@"parserDidStartDocument");
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
////////NSLog(@"parseErrorOccurred");
NSString * errorString = [NSString stringWithFormat:@"Error (Error code %i )", [parseError code]];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading data" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:    (NSDictionary *)attributeDict
{
////NSLog(@"didStartElement");
////NSLog(@"elementName : %@",elementName);
////NSLog(@"namespaceURI : %@",namespaceURI);
////NSLog(@"qualifiedName : %@",qualifiedName);
////NSLog(@"attributeDict : %@",attributeDict);
[registerNewArr addObject:attributeDict];

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
////NSLog(@"foundCharacters");
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
/////NSLog(@"didEndElement");   
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
}
-(void)startParsingForLogin:(NSString *)userIdStr Password:(NSString *)passwordStr 
{

NSString *urlString = [NSString stringWithFormat:@"http://www.example.com/loginxml.php?username=%@&password=%@",userIdStr,passwordStr];
////////NSLog(@"urlString : %@",urlString);
NSURL *xmlURL = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:xmlURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]autorelease];

NSURLResponse *returnedResponse = nil;
NSError *returnedError = nil;
NSData *itemData  = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnedResponse error:&returnedError];
//NSString *itemString = [[[NSString alloc] initWithBytes:[itemData bytes] length:[itemData length] encoding:NSUTF8StringEncoding]autorelease];

//////NSLog(@"itemString : %@",itemString);


xmlParser = [[NSXMLParser alloc] initWithData:itemData];        
[xmlParser setDelegate:self];

[xmlParser parse];

}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
////////NSLog(@"parserDidStartDocument");
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
////////NSLog(@"parseErrorOccurred");
NSString * errorString = [NSString stringWithFormat:@"Error (Error code %i )", [parseError code]];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading data" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:    (NSDictionary *)attributeDict
{
////NSLog(@"didStartElement");
////NSLog(@"elementName : %@",elementName);
////NSLog(@"namespaceURI : %@",namespaceURI);
////NSLog(@"qualifiedName : %@",qualifiedName);
////NSLog(@"attributeDict : %@",attributeDict);
[registerNewArr addObject:attributeDict];

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
////NSLog(@"foundCharacters");
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
/////NSLog(@"didEndElement");   
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文