2010-12-08 19 views
1

私はアプリを作成しています。 HTTP POSTメソッドを使用してログイン情報を送信しています。サーバーから取得したreplyI'mはHTML形式です。どのようにしてそのHTMLを解析し、継承や失敗のための異なるメソッドを追加できますか?私が達成しようとしているのは、ログインに失敗するとUIAlerViewを使ってメッセージが表示され、ログインに成功すると、アプリケーションはアニメーションでビューを変更する必要があります。 :)HTML応答を解析する - iPhone App

私が今使っているコード:私は、私はHTMLparserクラスを使用し、正確に何をしたか

- (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]; 
} 
+0

これは広い質問です。まず第一に、HTMLを解析するのはおそらくここに行く方法ではありません。もしあなたがそれを行うことができれば、SOAPタイプのものが望ましいでしょう。なぜなら、HTMLの変化からログインを分離するほうが良いからです。第2に、裸のパスワードを投稿することは、HTTPSを使用しない限り悪い考えです。第三に、この質問を参照してください:http://stackoverflow.com/questions/405749/parsing-html-on-the-iphone – Robert

+0

ありがとうロバート、私はそれを選択できるように答えとしてこれを投稿してくださいなぜですか? :) –

答えて

0

。このクラスは、HTML形式の応答を受け取っている場合に非常に便利です。

0
-(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 
{ 
} 
関連する問題