我正在iphone应用程序中使用CXML解析XML,当我要搜索的位置(使用查询字符串)是一个单词时,效果很好。但是,当我添加一个空间(比如我正在寻找鞋店)时,它就会掉下来。我尝试用%20替换“”空间,但解析后似乎无法读取该URL。
我的代码:
- (IBAction)doSearch {
NSString *trimmedWhat = [txtboxWhat.text stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSString *trimmedWhere = [txtboxWhere.text stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *tempFullUrl = [NSString stringWithFormat:@"http://sampleurl.com/mobilesearch/place/%@/0/0/0/%@/0/0/0/0/0/0/0/0/search.aspx", trimmedWhat, trimmedWhere];
searchType = @"fullSearch";
NSLog(@"Full String: %@",tempFullUrl);
NSLog(@"Search Type: %@",searchType);
PromotionViewController *passArray = [[PromotionViewController alloc] initWithNibName:@"PromotionViewController" bundle:nil];
[passArray setCurrentCat: tempFullUrl];
[passArray setCurrentType: searchType];
[self.navigationController pushViewController:passArray animated:YES];
[PromotionViewController release];
}
然后在我的PromotionViewController上:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString: [NSString stringWithFormat:[NSString stringWithFormat:@"%@", currentCat]]]];
[request setHTTPMethod: @"GET"];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
stringReplyServer = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
if(currentType == @"categorySearch") {
...do parse
}
返回位置的网址时似乎就掉了
如何清理URL?
汤姆
编辑
我已将以下内容添加到我的原始搜索过程中
NSString *utfString = [tempFullUrl UTF8String];
PromotionViewController *passArray = [[PromotionViewController alloc] initWithNibName:@"PromotionViewController" bundle:nil];
[passArray setCurrentCat: utfString];
[passArray setCurrentType: searchType];
[self.navigationController pushViewController:passArray animated:YES];
[PromotionViewController release];
但是它归结为以下内容:
2011-11-18 10:41:52.677 Del Search[2312:f203] Full String: http://web-xml.asdasdas.com/mobilesearch/place/(null)/0/0/0/<UITextField: 0x74709d0; frame = (15 7; 286 31); text = 'london'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7470af0>>/0/0/0/0/0/0/0/0/search.aspx
最佳答案
- (IBAction)doSearch {
NSString * trimmedWhat = [txtboxWhat.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString * trimmedWhere = [txtboxWhat.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *tempFullUrl = [NSString stringWithFormat:@"http://sampleurl.com/mobilesearch/place/%@/0/0/0/%@/0/0/0/0/0/0/0/0/search.aspx", trimmedWhat, trimmedWhere];
searchType = @"fullSearch";
NSLog(@"Full String: %@",tempFullUrl);
NSLog(@"Search Type: %@",searchType);
PromotionViewController *passArray = [[PromotionViewController alloc] initWithNibName:@"PromotionViewController" bundle:nil];
[passArray setCurrentCat: tempFullUrl];
[passArray setCurrentType: searchType];
[self.navigationController pushViewController:passArray animated:YES];
[PromotionViewController release];
}
NSString *utfString = [currentCat UTF8String];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString: utfString]];
[request setHTTPMethod: @"GET"];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
stringReplyServer = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
if(currentType == @"categorySearch") {
...do parse
}
关于iphone - 解析XML iPhone时清理URLS(主要用于空格),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8180506/