1

我从网络服务获取这个字符串“Rasberry & Banana Smoothies”,它把字符串作为三个不同的值而不是一个值:

覆盆子 香蕉
冰沙

如果我&从服务器数据库中删除符号,它会按原样获取完整的字符串。

我不明白为什么会这样,有人可以指导我吗?

提前致谢。

在这里,我发布了获取它的代码,这可能有助于理解问题:

- (void)viewDidLoad
{
[super viewDidLoad];

arrayTitle = [[NSMutableArray alloc]init];
arrayImage = [[NSMutableArray alloc]init];
arrayRecipe = [[NSMutableArray alloc]init];

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"2-right-side-sheet-1.png"]];

NSString *url=[[NSString alloc]initWithFormat:@"http://smoktec.net/Monty/api/?"];

NSString *Action=@"action=";

NSString *compUrl=[[NSString alloc]initWithFormat:@"%@%@%@",url,Action,@"Recipes"];

NSURL *urlToPars= [NSURL URLWithString:compUrl];
NSLog(@"%@",compUrl);

[self parseWithUrl3:urlToPars];
}

-(void)parseWithUrl3:(NSURL *)compUrl
{
    parser=[[NSXMLParser alloc]initWithContentsOfURL:compUrl];
    [parser setDelegate:self];
    [parser parse];
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    stcurrentelement=[NSString stringWithFormat:@"%@",elementName];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

    if(!(stcurrentelement==nil))
    {
        if([stcurrentelement isEqualToString:@"Recipetitle"])
        {
            [arrayTitle addObject:string];
            NSLog(@"%@",string);
        }

        else if([stcurrentelement isEqualToString:@"RecipeImg"])
        {
            [arrayImage addObject:string];
            NSLog(@"%@",string);
        }

        else if([stcurrentelement isEqualToString:@"Recipeid"])
        {
            [arrayRecipe addObject:string];
            NSLog(@"%@",string);
        }
    }
}
4

3 回答 3

1

尝试这个:

NSString *compUrl=[[NSString alloc]initWithFormat:@"%@%@%@",url,Action,@"Recipes"];
NSString *URLEncodedText = [compURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

您需要在发出请求之前对 URL 字符串进行编码。

于 2012-12-26T08:41:26.037 回答
0

最后是解决方案,在从服务器接收数据后连接字符串。

NSString *str = [arrayTitle objectAtIndex:3];
NSString *str1 = [arrayTitle objectAtIndex:4];
NSString *str2 = [arrayTitle objectAtIndex:5];

NSString *strring = [str stringByAppendingString:str1];
 NSString *strring1 = [strring stringByAppendingString:str2];

NSLog(@"strr:%@",strring1);
于 2012-12-26T10:39:22.447 回答
0

您应该对 Web 服务结果/响应进行编码,然后重试。

于 2012-12-26T08:32:47.407 回答