iphone:NSMutableURLRequest 返回 MS Word 样式撇号的奇怪字符

发布于 2024-10-14 09:42:23 字数 838 浏览 4 评论 0原文

我们使用 XML/NSMutableURLRequest 从我们的网站中提取内容,有时它会通过“卷曲”样式的撇号和引号(“而不是”)提取内容。 NSMutableURLRequest 似乎讨厌这些并将它们变成奇怪的 \U00e2\U0080\U0099 字符串。

我可以采取什么措施来防止这种情况发生吗?我正在使用 GET 方法,那么我应该以某种方式告诉它使用 UTF-8 吗?或者,我错过了什么吗?

UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = YES;

    NSString *urlStr = [NSString stringWithFormat:@"%@",url];
    NSURL *serviceUrl = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
    [serviceRequest setHTTPMethod:@"GET"];

    NSURLResponse *serviceResponse;
    NSError *serviceError;

    app.networkActivityIndicatorVisible = NO;

    return [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];

We are pulling content off our website using XML/NSMutableURLRequest and sometimes it pulls through the "curly" style apostrophe and quotes, ’ rather than '. NSMutableURLRequest seems to hate these and turns them into the strange \U00e2\U0080\U0099 string.

Is there something that I can to do prevent this? I am using the GET method, so should I be somehow telling it to use UTF-8? Or, am I missing something?

UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = YES;

    NSString *urlStr = [NSString stringWithFormat:@"%@",url];
    NSURL *serviceUrl = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
    [serviceRequest setHTTPMethod:@"GET"];

    NSURLResponse *serviceResponse;
    NSError *serviceError;

    app.networkActivityIndicatorVisible = NO;

    return [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吝吻 2024-10-21 09:42:23

NSURLConnection 返回一个 NSData 响应。您可以获取 NSData 响应并将其转换为字符串。然后获取该字符串,将其转回 NSData 对象,一路上对其进行正确的 UTF-8 编码,并将其提供给 NSXMLParser。

示例:(假设 response 是您请求的 NSData 响应)

// long variable names for descriptive purposes
NSString* xmlDataAsAString = [[[NSString alloc] initWithData:response] autorelease];
NSData* toFeedToXMLParser = [xmDataAsAString dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser* parser = [[[NSXMLParser alloc] initWithData:toFeedToXMLParser] autorelease];
// now utilize parser...

NSURLConnection returns an NSData response. You can take that NSData response and turn it into a string. Then take this string, turn it back into a NSData object, properly UTF-8 encoding it along the way, and feed it to NSXMLParser.

Example: (Assuming response is the NSData response from your request)

// long variable names for descriptive purposes
NSString* xmlDataAsAString = [[[NSString alloc] initWithData:response] autorelease];
NSData* toFeedToXMLParser = [xmDataAsAString dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser* parser = [[[NSXMLParser alloc] initWithData:toFeedToXMLParser] autorelease];
// now utilize parser...
您的好友蓝忘机已上羡 2024-10-21 09:42:23

我建议使用 stringByReplacingCharactersInRange:withString: 替换这些字符来替换不需要的字符串。

NSString *currentTitle = @"Some string with a bunch of stuff in it.";

//Create a new range for each character.
NSRange rangeOfDash = [currentTitle rangeOfString:@"character to replace"];
NSString *location = (rangeOfDash.location != NSNotFound) ? [currentTitle substringToIndex:rangeOfDash.location] : nil;

if(location){
    currentTitle = [[currentTitle stringByReplacingOccurrencesOfString:location withString:@""] mutableCopy];
}

我过去曾这样做过,以解决您所描述的相同问题。

I would suggest replacing those characters using stringByReplacingCharactersInRange:withString: to replace the unwanted strings.

NSString *currentTitle = @"Some string with a bunch of stuff in it.";

//Create a new range for each character.
NSRange rangeOfDash = [currentTitle rangeOfString:@"character to replace"];
NSString *location = (rangeOfDash.location != NSNotFound) ? [currentTitle substringToIndex:rangeOfDash.location] : nil;

if(location){
    currentTitle = [[currentTitle stringByReplacingOccurrencesOfString:location withString:@""] mutableCopy];
}

I've done this in the past to handle the same problem you describe.

眼泪淡了忧伤 2024-10-21 09:42:23

尝试使用 stringByReplacingPercentEscapesUsingEncoding:

Try using the stringByReplacingPercentEscapesUsingEncoding:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文