HttpResponse.getEntity()。getContent()返回所有内容 ...包括响应标头,javascript代码,响应正文(当然!)等。
是否有清除此功能的功能并仅提供响应正文?
答案 0 :(得分:1)
您必须将InputStream中的数据读取到缓冲区。搜索此正则表达式:
\r\n\r\n(.*)
这将为您提供标题后的内容。
如果您要搜索:
,可以用空字符串替换它^.*?\r\n\r\n
答案 1 :(得分:1)
您可以使用自己的方法使用Android过滤模式。传入字符串,并应用模式过滤器来删除您不想要的内容。
public String filter(String searchString)
{
String content = searchString;
// Remove the new line characters.
Pattern newLineChar = Pattern.compile("\n+");
Matcher mLine = newLineChar.matcher(content);
while (mLine.find())
content = mLine.replaceAll("");
// Return the clean content
return content;
}
您的模式可能会非常复杂,并且几乎可以过滤您想要的任何表达式。 (您可能需要使用正则表达式等)。上面的示例用0长度字符串替换新行(\ n),以从字符串中删除所有这些行。您可以构建模式,也可以再次迭代以删除其他内容。
您还需要一些导入才能实现此目的:
import java.util.regex.Matcher;
import java.util.regex.Pattern;