ASIHTTPRequest:检测到不正确的 NSStringEncoding 值 0x0000

发布于 2024-12-02 04:25:08 字数 235 浏览 1 评论 0原文

检测到不正确的 NSStringEncoding 值 0x0000。假设 NSStringEncodingASCII。将停止此兼容性映射行为 不久的将来。

当我使用 ASIHTTPRequest 时,我不断收到此错误(50% 的时间),这是怎么回事?

我假设我传入的 URL 是正确的,因为它不包含任何空格或奇怪的字符,也许结果字符串包含一些无法识别的字符?

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

When I was using ASIHTTPRequest, I keep getting this error(50% of the times), what's wrong with it?

I assume the URL I passed in is correct, as it does not contain any space or strange character, maybe it is the result string having some unrecognized character?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋意浓 2024-12-09 04:25:08

https://stackoverflow.com/q/8251175/918764 复制我的答案

错误并不是您的数据不是'如果编码不正确,几乎可以肯定您正在从尚未设置编码的 ASIHTTPRequest 对象请求某种字符串数据 - 如果出现这种情况,就会发生这种情况请求无法连接到服务器,或者服务器未发送可理解的编码标头。


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

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

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

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

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

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

Copying my answer from https://stackoverflow.com/q/8251175/918764

The error is not that your data isn't encoded correctly, it's almost certainly that you're requesting some kind of string data from an ASIHTTPRequest object that doesn't have an encoding set yet - this will happen if the request failed to connect to the server, or if the server didn't send an understandable encoding header.


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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文