iPhone:NSMutableURLRequest返回MS Word样式撇号的奇怪字符 [英] iphone: NSMutableURLRequest returns strange characters for MS Word style apostrophe
问题描述
我们正在使用XML/NSMutableURLRequest从我们的网站上提取内容,有时它会提取卷曲"样式的单引号和引号,而不是'.NSMutableURLRequest似乎讨厌这些并将它们变成奇怪的\ U00e2 \ U0080 \ U0099字符串.
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.
我有什么办法可以防止这种情况发生?我正在使用GET方法,所以我应该以某种方式告诉它使用UTF-8吗?或者,我想念什么吗?
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];
推荐答案
NSURLConnection
返回 NSData
响应.您可以使用该 NSData
响应并将其转换为字符串.然后将此字符串取回,将其返回为 NSData
对象,并在此过程中对其进行适当的UTF-8编码,然后将其提供给 NSXMLParser
.
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
.
示例:(假设 response
是您请求中的 NSData
响应)
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...
这篇关于iPhone:NSMutableURLRequest返回MS Word样式撇号的奇怪字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!