我有从电子邮件服务器获取的数据我想消除噪音并解析传入数据的日期和主题并将其输出到文本视图中的用户
我们应该如何从电子邮件服务器获取数据?
传入数据就像
something....
something...
* OK [PERMANENTFLAGS ($Submitted $XAOL-SENT \Answered \Deleted \Draft \Flagged \Seen $Forwarded $MDNSent Forwarded XAOL-GOOD XAOL-GOODCHECK-DONE)] Permanent flags
. OK [READ-WRITE] SELECT completed
. fetch 1:* (body[header.fields (from subject date)])
* 1 FETCH (BODY[HEADER.FIELDS (FROM SUBJECT DATE)] {149}
Date: Mon, 07 Nov 2011 17:00:25 -0500 (EST)
From: "AOLWelcomeInfo" <AOLWelcomeInfo@message.aol.com>
Subject: Welcome to Your New Email Account!
)
. OK FETCH completed
我使用此代码解析它
NSMutableData* data = [[NSMutableData alloc] init];
unsigned char c;
for (;;) {
recv(socket_, &c, sizeof(c), 0);
if (c == '\n') {
NSString* s = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
NSRange range = [s rangeOfString:@"FETCH "];
if( range.location != NSNotFound ) {
NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:s];
[scanner scanUpToString:@"}" intoString:nil];
while(![scanner isAtEnd]) {
NSString *substring = nil;
[scanner scanString:@"}" intoString:nil];
if([scanner scanUpToString:@"*" intoString:&substring]) {
// If the space immediately followed the }, this will be skipped
[substrings addObject:substring];
}
[scanner scanUpToString:@")" intoString:nil]; // Scan all characters before next }
}
NSString *email;
[emailList addObject:@"Select an Email"];
for(int i=0; i<substrings.count;i++){
email = [substrings objectAtIndex:i];
[emailList addObject:email]; // add emails in emailList
}
NSLog(@"%@",emailList);
[substrings release];
}
我的NSlog输出就像
"Select an email"
"Select an email"
当我调试它时输入if语句,但我无法弄清楚为什么它没有正确解析它。
应该是这样的:
"Select an email"
Date: Mon, 07 Nov 2011 17:00:25 -0500 (EST)
From: "AOLWelcomeInfo" <AOLWelcomeInfo@message.aol.com>
Subject: Welcome to Your New Email Account!)
答案 0 :(得分:0)
问题是我每次都在for(;;){}
读取一行数据,所以每当NSString S得到一行而不解析它时,我删除了内部的所有内容,我将整个解析方法更改为