使用ASIHttpRequest编码问题 [英] Encoding issue with ASIHttpRequest

查看:124
本文介绍了使用ASIHttpRequest编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ASIHttpRequest的编码问题。当我得到一个URL时,除了一点编码问题外,数据会被完美地返回。

I have an encoding problem with ASIHttpRequest. When I get an URL, the data is returned perfectly except for a little encoding problem.

这是我的代码:

- (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> ,尽管在​​浏览器中显示页面时它是正确的:

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?

推荐答案

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

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

NSData *data = request.responseData;

这篇关于使用ASIHttpRequest编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆