html代码查看器

html代码查看器
1.新建一个Android工程  
2.构建布局文件.添加网络访问权限
4.在主Activity中实现点击事件
   public void viewHtml(View view) {
String path = et_path.getText().toString();
try {
String content = HtmlService.getHtmlContent(path);
if (content != null) {
tv_content.setText(content);
}else {
tv_content.setText("获取数据失败");
}
} catch (Exception e) {
e.printStackTrace();
tv_content.setText("获取数据失败");
}
   }
5.创建Service包中的HtmlService类
   public static String getHtmlContent(String path) throws Exception{
URL url = new URL(path);
HttpURLConnection  conn =  (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
InputStream is = conn.getInputStream();
    byte[] data = StreamTool.getStreamBytes(is);
    return new String(data);
}
return null;
   }
   如果出现乱码,可以先查看出网页使用是哪种编码格式.然后在return new String(data,"编码格式")中指定.
6.创建工具包中的StreamTool类
    //把inputstream的信息 转化成byte[] 返回
    public static byte[] getStreamBytes(InputStream is) throws Exception{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = is.read(buffer))!=-1 ){
baos.write(buffer, 0, len);
}
byte[] result = baos.toByteArray();
is.close();
baos.close();
return result;
    }

7.获取到的内容过多在TextView中显示不去的话.可以定义个ScrollView控件,把TextView放进去即可.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值