ASIHttpRequest 的编码问题
我有 ASIHttpRequest 的编码问题。当我得到一个 URL 时,除了一点编码问题之外,数据都完美返回。
这是我的代码:
- (void)fetchGamesForCategory
{
NSString *url_string = [[NSString alloc] initWithFormat:url_match, theCategory._id];
NSURL *url = [NSURL URLWithString:url_string];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(@"response: %@", response);
NSData *data = [response dataUsingEncoding:NSUTF8StringEncoding];
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSDictionary * dict = (NSDictionary*)[NSPropertyListSerialization
propertyListFromData:data
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
NSDictionary *categoryListPathDictionary = [[NSDictionary alloc] initWithDictionary:dict];
categoryMatchList *categoryMatchListFile = [[[categoryMatchList alloc] initWithDictionary:categoryListPathDictionary] retain];
matchArray = [categoryMatchListFile getMatchListXmlSet];
[self loadPage];
}
}
这是我的结果:
2010-09-28 21:49:35.970 oddsApp[46429:190f] response: <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist>
<dict>
<key>matches</key>
<array>
<dict>
<key>name</key>
<string>Gais - Häcken</string>
<key>date</key>
<string>2010-09-29 18:00</string>
<key>id</key>
<string>156</string>
<key>odds</key>
<dict>
<key>1</key>
<string>2.6</string>
<key>X</key>
<string>3.28</string>
<key>2</key>
<string>2.862</string>
</dict>
</dict>
</array>
</dict>
</plist>
如您所见,编码在
处搞砸了,尽管在显示时是正确的浏览器中的页面:
有人知道出了什么问题吗?
I have an encoding problem with ASIHttpRequest. When I get an URL, the data is returned perfectly except for a little encoding problem.
This is my code:
- (void)fetchGamesForCategory
{
NSString *url_string = [[NSString alloc] initWithFormat:url_match, theCategory._id];
NSURL *url = [NSURL URLWithString:url_string];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(@"response: %@", response);
NSData *data = [response dataUsingEncoding:NSUTF8StringEncoding];
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSDictionary * dict = (NSDictionary*)[NSPropertyListSerialization
propertyListFromData:data
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
NSDictionary *categoryListPathDictionary = [[NSDictionary alloc] initWithDictionary:dict];
categoryMatchList *categoryMatchListFile = [[[categoryMatchList alloc] initWithDictionary:categoryListPathDictionary] retain];
matchArray = [categoryMatchListFile getMatchListXmlSet];
[self loadPage];
}
}
And this is my result:
2010-09-28 21:49:35.970 oddsApp[46429:190f] response: <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist>
<dict>
<key>matches</key>
<array>
<dict>
<key>name</key>
<string>Gais - Häcken</string>
<key>date</key>
<string>2010-09-29 18:00</string>
<key>id</key>
<string>156</string>
<key>odds</key>
<dict>
<key>1</key>
<string>2.6</string>
<key>X</key>
<string>3.28</string>
<key>2</key>
<string>2.862</string>
</dict>
</dict>
</array>
</dict>
</plist>
As you can see, the encoding is screwed up at <string>Gais - Häcken</string>
, allthough it is correct when showing the page in the browser:
Does someone know what is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果不先将响应转换为字符串怎么办?
What if you don't convert the response to a string first?
我遇到了类似的问题,这为我解决了:
请参阅此处的文档:
https://developer. apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
我的实际上是用于以类似于 URL 编码的任何内容开头的帖子值,就像此列表中的任何内容一样:
http://www.w3schools.com/tags/ref_urlencode.asp
I had a similar problem and this fixed it for me:
See documentation here:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
Mine was actually for post values that started with anything resembling URL Encoding like say anything on this list:
http://www.w3schools.com/tags/ref_urlencode.asp