检测到不正确的NSStringEncoding值0x0000(使用ASIHTTPRequest) [英] Incorrect NSStringEncoding value 0x0000 detected (using ASIHTTPRequest)

查看:142
本文介绍了检测到不正确的NSStringEncoding值0x0000(使用ASIHTTPRequest)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用ASIHTTPRequst并且遇到了一些问题,但仅限于某些设备。在某些情况下,我收到以下错误:

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:


检测到错误的NSStringEncoding值0x0000。假设NSASCIIStringEncoding。将在近处停止此兼容性映射行为未来

我测试这个有限的选项,因为它适用于我所有的内部测试设备,但问题仍然发生在我的一个或多个客户测试手机。也可能是手机连接到蜂窝网络的情况,因为它似乎正在使用wifi。

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.

有谁知道可能导致问题的原因?我看到有相似类型的问题,但我的代码看起来都是正确的,从其他问题答案和ASIHTTPRequest网站。

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代码段

    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
}


推荐答案

https://stackoverflow.com/q/复制我的回答8251175/918764 - 您最有可能收到此消息,因为您在失败的请求上调用 getResponseString 。你应该确保在调用之前检查是否有响应:我发现测试总请求失败的一种方法(即网络/服务器关闭)是检查:

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
}






这显着如果在返回有效响应并且已设置请求编码(即无法连接到服务器)之前调用 [asiHttpRequest getResponseString] ,则会发生这种情况。


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).

解决此警告的最简单方法是编辑 ASIHTTPRequest 类,删除 @synthesize responseEncoding 并添加一个简单的自定义getter / setter,以便在未设置响应编码时返回默认编码:

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;
}

getResponseString <还有一个更具体的解决方法/ code>方法,我认为这是唯一使用编码而不检查值的地方 - 因为编码应该设置为任何非零长度响应:

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];
}

这篇关于检测到不正确的NSStringEncoding值0x0000(使用ASIHTTPRequest)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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