0

我在我的 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
4

2 回答 2

1
- (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
}
于 2011-11-18T11:28:49.633 回答
1

为了解决它,我刚刚做了:

NSString *utfString = [currentCat stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
于 2011-11-21T09:23:43.333 回答