下面这段代码让我很困惑:
// Get the content text
String contentText = null;
Header contentEncodingHeader = m_httpEntity.getContentEncoding();
final String contentEncodingValue = contentEncodingHeader != null ? contentEncodingHeader.getValue() : ""; // In my example, this is set to "gzip"
if (contentEncodingValue == "")
{
contentText = this.GetResponseContentText(inputStream, charset);
}
else if (contentEncodingValue == "gzip")
{
contentText = this.GetResponseContentText_GZip(inputStream, charset);
}
return contentText;
当我遍历代码行时,它按以下顺序执行:
1) if (contentEncodingValue == "")
{
3) contentText = this.GetResponseContentText(inputStream, charset);
}
2) else if (contentEncodingValue == "gzip")
{
contentText = this.GetResponseContentText_GZip(inputStream, charset);
}
4) return contentText;
更奇怪的是,它甚至没有进入GetResponseContentText
函数。我真的很困惑。有人能解释一下吗?
同样,如果我注释掉if语句,它会正常工作(进入GetResponseContentText_GZip
函数)。
从字符串比较,您将希望使用equals
而不是==
if (contentEncodingValue.equals("")) {
...
}
else if (contentEncodingValue.equals("gzip")) {
...
}