我正在使用以下代码从我的网站(在我的Android应用中)请求一些内容:
response = httpClient.execute(httpPost, localContext);
// Pull content stream from response
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
InputStreamReader rd = new InputStreamReader(inputStream);
//ByteArrayOutputStream content = new ByteArrayOutputStream();
StringBuilder content = new StringBuilder();
int n = 0;
char[] buffer = new char[40000];
while (n >= 0)
{
n = rd.read(buffer, 0, buffer.length);
if (n > 0)
{
content.append(buffer, 0, n);
}
}
ret = content.toString();
网站的内容采用Windows-1251
编码进行编码,但在应用中我收到了一堆不可读的符号,如下所示:
此外,以下代码段似乎也不起作用:
text = new String(matcher.group(1).getBytes("Windows-1251"), "UTF-8");
答案 0 :(得分:1)
试试这个,
BufferedReader reader = new BufferedReader(new InputStreamReader(in_stream), "UTF-8");
StringBuilder content = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
content.append(line + "\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
in_stream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
ret = content.toString();