ASIHttpRequest 的编码问题

发布于 2024-09-25 06:59:39 字数 2731 浏览 4 评论 0原文

我有 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>

如您所见,编码在 Gais - Häcken 处搞砸了,尽管在显示时是正确的浏览器中的页面: FireFox snapshot

有人知道出了什么问题吗?

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:
FireFox screenshot

Does someone know what is wrong?

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

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

发布评论

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

评论(2

缘字诀 2024-10-02 06:59:39

如果不先将响应转换为字符串怎么办?

NSData *data = request.responseData;

What if you don't convert the response to a string first?

NSData *data = request.responseData;
清泪尽 2024-10-02 06:59:39

我遇到了类似的问题,这为我解决了:

stringByAddingPercentEscapesUsingEncoding

请参阅此处的文档:
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:

stringByAddingPercentEscapesUsingEncoding

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

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