Android通过访问网页查看网页源码实例详解
作者:lqh 时间:2023-10-05 03:09:11
Android通过访问网页查看网页源码
1.添加网络权限
<!--访问网络的权限-->
<uses-permission android:name="android.permission.INTERNET"/>
2.获取网络中网页的数据
/**
* 获取网页HTML源代码
* @param path 网页路径
*/
public static String getHtml(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 inStream=conn.getInputStream();
byte[] data=read(inStream);
String html=new String(data,"UTF-8");
return html;
}
return null;
}
/**
* 读取流中的数据
*/
public static byte[] read(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte[] b=new byte[1024];
int len=0;
while((len=inputStream.read(b))!=-1){
outputStream.write(b);
}
inputStream.close();
return outputStream.toByteArray();
}
3.处理查看网页源码的控制
public class HtmlViewActivity extends Activity {
private EditText pathText;
private TextView codeView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pathText=(EditText) findViewById(R.id.pagepath);//网页路径
codeView=(TextView)findViewById(R.id.codeView);//显示获得的源码
Button button=(Button) findViewById(R.id.button);//查看按钮
button.setOnClickListener(new ButtonClickListener());//按钮事件
}
/**
* 查看按钮处理事件
*/
private final class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
String path=pathText.getText().toString();
try {
String html=PageService.getHtml(path);
codeView.setText(html);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.error, 1);
}
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/lht0211/article/details/8828454
标签:Android,网页源码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于SSM框架+Javamail发送邮件的代码实例
2022-04-28 11:59:04
![](https://img.aspxhome.com/file/2023/6/110916_0s.png)
Android短信接收监听、自动回复短信操作例子
2022-12-04 20:07:50
Java用三元运算符判断奇数和偶数的简单实现
2023-07-24 16:54:03
![](https://img.aspxhome.com/file/2023/4/97944_0s.png)
spring boot中多线程开发的注意事项总结
2022-03-14 19:20:07
Java实现多文件压缩加密并重命名压缩文件对象的方法
2021-07-02 15:44:31
![](https://img.aspxhome.com/file/2023/0/108190_0s.png)
Android中Listview点赞功能的实现
2023-06-21 11:49:59
Java中Validated、Valid 、Validator区别详解
2023-11-11 13:53:31
Java 从互联网上爬邮箱代码示例
2022-02-27 16:40:57
WPF中使用WebView2控件的方法及常见问题
2023-10-02 18:23:28
![](https://img.aspxhome.com/file/2023/6/119496_0s.png)
Android图片框架Glide原理深入探索
2021-07-22 05:08:42
C#计算器编写代码
2022-12-28 02:57:25
![](https://img.aspxhome.com/file/2023/5/108635_0s.gif)
Android如何在Gradle中更改APK文件名详解
2021-06-02 12:59:31
Java享元设计模式优化对象创建提高性能和效率
2022-05-26 23:12:02
![](https://img.aspxhome.com/file/2023/3/65633_0s.png)
C#中TextBox的横线样式及占位提示详解
2023-05-17 10:33:27
![](https://img.aspxhome.com/file/2023/8/113838_0s.png)
Ubuntu 20.04 下安装配置 VScode 的 C/C++ 开发环境(图文教程)
2021-12-01 16:48:23
![](https://img.aspxhome.com/file/2023/3/123853_0s.png)
Android自定义View原理(实战)
2021-07-25 02:46:47
![](https://img.aspxhome.com/file/2023/1/139271_0s.png)
maven项目install时忽略执行test方法的总结
2023-11-07 09:58:16
![](https://img.aspxhome.com/file/2023/5/77455_0s.jpg)
springboot相关面试题汇总详解
2023-10-06 17:16:11
快速理解Java设计模式中的组合模式
2021-10-18 04:16:29
![](https://img.aspxhome.com/file/2023/3/86733_0s.jpg)
Java 实现简单静态资源Web服务器的示例
2021-06-16 17:12:55