问题描述
当我使用ASIHTTPRequest时,我不断得到这个错误50%的时间),怎么了?
我认为我传递的URL是正确的,因为它不包含任何空格或奇怪的字符,也许它结果字符串是否具有一些无法识别的字符?
从对象的某些字符串数据 - 如果请求失败,会发生这种情况连接到服务器,或者服务器没有发送可理解的编码头。
通话 [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;
}
// ---插入这个块---
//如果'data'存在但为空,则返回一个简单的空字符串
if(data.length == 0){
return @;
}
//assert(self.responseEncoding); //如果你进入运行时断言,取消注释这个
// --- END OF BLOCK TO INSERT ---
return [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:[self responseEncoding]] autorelease];
}
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?
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]; }
这篇关于ASIHTTPRequest:检测到不正确的NSStringEncoding值0x0000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!