Санитарная обработка URL-адресов (в основном для пробелов) при разборе XML iPhone - PullRequest
0 голосов
/ 18 ноября 2011

Я анализирую XML с помощью CXML в своем приложении для iphone, работает нормально, когда локация, которую я ищу (используя строку запроса), состоит из одного слова.Однако, когда я добавляю пробел (скажем, я ищу обувной магазин), он падает.Я попытался заменить пробел "" на% 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 для места

Как очистить 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

Ответы [ 2 ]

1 голос
/ 21 ноября 2011

Чтобы это исправить, я просто сделал:

NSString *utfString = [currentCat stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
1 голос
/ 18 ноября 2011
- (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
}
...