Question

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?

Was it helpful?

Solution

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

NSData *data = request.responseData;

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top