自做了一个android小程序,用于访问本地的一个web项目下的一个网页。
用浏览器去访问该该网页能够正常显示中文,而用android程序通过流去读取该网页,
中文会被 "南少林" 之类的代码替代。
<%@ page language="java" import="java.util.*" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s" %><s:property value="str"/>web网页如上: <s:property value="str"/> 通过浏览器访问会显示如下语句
[{id:110,name:"太极宗师",time:28},{id:111,name:"南少林",time:38},{id:112,name:"楚留香",time:30}] 通过 android 程序读取网页信息代码如下: public static String getHtml(String textsrc) throws Exception {
URL url=new URL(textsrc);
HttpURLConnection URLConnection=(HttpURLConnection) url.openConnection();
URLConnection.setConnectTimeout(5000);
URLConnection.setRequestMethod("GET");
byte by[];
if(URLConnection.getResponseCode()==200)
{
InputStream inputStream=URLConnection.getInputStream();
by=Inpututil.getinput(inputStream);
inputStream.close();
return new String(by);
}
return null;
}
public class Inpututil { public static byte[] getinput(InputStream input) throws Exception {
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte by[]=new byte[1024];
int len=0;
while((len=input.read())!=-1)
{
input.read(by);
outputStream.write(by);
}
input.close();
byte bt[]=outputStream.toByteArray();
System.out.println(new String(bt,"GBK"));//想在这里输出与浏览器中一样的内容,可输出乱码,求解
return bt;
}}
用浏览器去访问该该网页能够正常显示中文,而用android程序通过流去读取该网页,
中文会被 "南少林" 之类的代码替代。
<%@ page language="java" import="java.util.*" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s" %><s:property value="str"/>web网页如上: <s:property value="str"/> 通过浏览器访问会显示如下语句
[{id:110,name:"太极宗师",time:28},{id:111,name:"南少林",time:38},{id:112,name:"楚留香",time:30}] 通过 android 程序读取网页信息代码如下: public static String getHtml(String textsrc) throws Exception {
URL url=new URL(textsrc);
HttpURLConnection URLConnection=(HttpURLConnection) url.openConnection();
URLConnection.setConnectTimeout(5000);
URLConnection.setRequestMethod("GET");
byte by[];
if(URLConnection.getResponseCode()==200)
{
InputStream inputStream=URLConnection.getInputStream();
by=Inpututil.getinput(inputStream);
inputStream.close();
return new String(by);
}
return null;
}
public class Inpututil { public static byte[] getinput(InputStream input) throws Exception {
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte by[]=new byte[1024];
int len=0;
while((len=input.read())!=-1)
{
input.read(by);
outputStream.write(by);
}
input.close();
byte bt[]=outputStream.toByteArray();
System.out.println(new String(bt,"GBK"));//想在这里输出与浏览器中一样的内容,可输出乱码,求解
return bt;
}}
解决方案 »
- PopupWindow上加载带ListView控件的窗口,如何使listview获取点击事件
- 关于使用RingtoneManager设置短信铃声的问题
- android应用软件如何存取MS SQL SERVER或 oracle数据库中的数据。
- android 2.2 如何退出程序
- 日期格式设置年/月/日,改为日/月/年,但是主界面显示没有变化
- 我想写一个公司通讯录的程序
- 求解实例
- 请教弧线滑动效果实现思路
- 求助:ListView中textview无法更新
- android 4.4源码下载——百度云盘地址
- android 如何让图片沿曲线运动啊- -
- 怎么做让SVN上的版本号在代码上传时自动修改AndroidMainfest版本号
把这句话转码看看吧!GBK和utf-8都试下。好久都没弄过JSP了,都忘的差不多了。