13

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
}
Madoc
  • 1,605
  • 3
  • 17
  • 30
  • 2
    You should show the code for your urlEncodeValue method – Tim Dean Nov 15 '11 at 14:27
  • 2
    thanks Tim, its includded now – Madoc Nov 15 '11 at 14:46
  • 1
    Everything looks good here as far as I can see: How sure are you that the problem is occurring in this part of the code? You say that no response is received from the server. Have you verified that the message is either being sent or not sent to the server? – Tim Dean Nov 15 '11 at 15:05
  • i've gone through the code step by step and the error message comes when the request times out at the line: ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; EDIT: However, this only occured on one of the test phones for my client, not anyone of our phones have this problem so it's very strange:\ I can't reproduce the problem now on our phones – Madoc Nov 15 '11 at 15:21
  • Can not solve this problems, any help is needed. I have problem when I fire every request either from simulator ir from device. – Arpit B Parekh Jul 12 '17 at 12:20

1 Answers1

15

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];
}
Community
  • 1
  • 1
rvalue
  • 2,652
  • 1
  • 25
  • 31
  • 1
    Glad to help! We have a few other changes like this to the ASIHTTPRequest code that we use, so I think I'll make a GitHub fork soon and commit them all there for the Greater Good. – rvalue Nov 28 '11 at 00:01
  • 1
    We ended up dropping ASIHTTPRequest because it has some critical flaws dealing with authenticating proxies in the wild; wrapping NSURLRequest/NSURLConnection directly gives us better performance and more flexibility for coping with custom SSL validation. Your mileage may vary. – rvalue Aug 28 '12 at 06:24
  • Good Answer rvalue. Please share the link of your GitHub fork. So, we can get the code which you have changed for ASIHTTPRequest – Asif Bilal Aug 20 '14 at 06:09
  • @AsifBilal Unfortunately my boss at the time wouldn't clear it; so I can't actually share the code. – rvalue Aug 21 '14 at 04:59
  • I did not declare request time out correctly, request failed because I was uploading video/photo so it takes time so request fails. – Arpit B Parekh Jul 14 '17 at 05:38