views:

29

answers:

1

Hi,

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"&gt;&lt;plist&gt;
    <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?

Best regards, Paul Peelen

+3  A: 

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

NSData *data = request.responseData;
Robot K
Good idea, I will.
Paul Peelen