我有ASIHttpRequest的編碼問題。當我得到一個URL時,除了一點編碼問題之外,數據會被完美地返回。ASIHttpRequest的編碼問題
這是我的代碼:
- (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>
正如你所看到的,編碼在<string>Gais - Häcken</string>
搞砸了,allthough它顯示的頁面時是正確的在瀏覽器中:
有人知道什麼是錯的嗎?
ASIHTTPRequest中的可愛評論:「//如果內容類型標頭指定編輯一個編碼,我們將使用它,否則我們使用defaultStringEncoding(默認爲NSISOLatin1StringEncoding)「。對於HTTP(文本編碼默認爲ISO-8859-1),這在技術上是正確的,但不符合其他人的操作。你可以做'[request setResponseEncoding:NSUTF8StringEncoding]',但這有點破解。 – 2010-09-28 23:57:05
是的,我讀到了,但我想我對它不夠了解。我搜索了一個「setEncoding」,但現在我知道爲什麼我沒有找到任何。謝謝。 – 2010-09-29 07:58:54