Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect NSStringEncoding value 0x0000 detected (using ASIHTTPRequest)

I'm using ASIHTTPRequst in my application and im having some problems, but only on certain devices. In some cases i get the following error:

" Incorrect NSStringEncoding value 0x0000 detected. Assuming NSASCIIStringEncoding. Will stop this compatiblity mapping behavior in the near future "

I got limited options for testing this out, because it works on all my internal test devices, but the problem still occurs on one or more of my clients test phones. It may also be a case when the phone is connected to the cellular network, as it seems to be working on wifi.

Does anyone know what might cause the problem? I see there are similar type of questions but my code looks like its correct both from the other questions answers and the ASIHTTPRequest site.

Edit When the error occurs no response from server is recieved so the user is stuck at register/login.

urlEncodeValue:

- (NSString *)urlEncodeValue:(NSString *)str
{
    NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8);
    return [result autorelease];
}

ASIHTTPRequest code snippet:

    NSString *encodedUrlAdress = [NSString stringWithFormat:@"%@user/register/%@/%@/%@/%d/%@/%@/%@/%@", 
                                   kUserServerAddress, 
                                   [self urlEncodeValue:_firstName], 
                                   [self urlEncodeValue:_lastName], 
                                   [self urlEncodeValue:_phoneNumber], 
                                   _sex, 
                                   [self urlEncodeValue:_birthDate], 
                                   [self urlEncodeValue:_address], 
                                   [self urlEncodeValue:_postalNumber]];    
    NSLog(@"encodedUrlAdress: %@", encodedUrlAdress);

    NSURL *url = [NSURL URLWithString:encodedUrlAdress];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    NSError *error = [request error];

    if (!error) {
// do stuff
}
like image 729
Madoc Avatar asked Nov 15 '11 13:11

Madoc


1 Answers

Copying my answer from https://stackoverflow.com/q/8251175/918764 - you're most likely getting this message because of you're calling getResponseString on a request that has failed. You should make sure that you check if you got a response before calling this: one way I've found to test for total request failure (i.e. network/server down) is to check:

if (request.responseStatusCode == 0) {
    // the request failed entirely
} else {
    // you at least got a HTTP response
}

This notably happens if you call [asiHttpRequest getResponseString] before a valid response has been returned and the request encoding has been set (i.e. couldn't connect to server).

The easiest way to workaround this warning is by editing the ASIHTTPRequest class, remove the @synthesize responseEncoding and adding a simple custom getter/setter so you can return the default encoding if the response encoding isn't set:

- (NSStringEncoding) responseEncoding
{
    return responseEncoding || self.defaultResponseEncoding;
}

- (void) setResponseEncoding:(NSStringEncoding)_responseEncoding
{
    responseEncoding = _responseEncoding;
}

There's also a more specific workaround for the getResponseString method, which I think is the only place that uses the encoding without checking for a value - since the encoding should be set for any non-zero length response:

- (NSString *)responseString
{
    NSData *data = [self responseData];
    if (!data) {
        return nil;
    }
    // --- INSERT THIS BLOCK ---
    // If the 'data' is present but empty, return a simple empty string
    if (data.length == 0) {
        return @"";
    }
    //assert(self.responseEncoding); // if you're into runtime asserts, uncomment this
    // --- END OF BLOCK TO INSERT ---
    return [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:[self responseEncoding]] autorelease];
}
like image 178
rvalue Avatar answered Oct 21 '22 05:10

rvalue