这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
这篇文章主要介绍了 Android 编程获取网址 html 代码的方法, 涉及 Android 针对给定 URL 地址的网页相关信息操作技巧, 需要的朋友可以参考下
本文实例讲述了 Android 编程获取网址 HTML 代码的方法。分享给大家供大家参考,具体如下:
- /**
- *
- * @param aUrl 网址
- * @param aEncode 编码
- * @return 返回的HTML代码
- * @throws Exception 对外抛出异常
- */
- public String getHTML(String aUrl, String aEncode) throws Exception {
- URL url = new URL(aUrl);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(5000);
- conn.setRequestMethod("GET");
- if (conn.getResponseCode() == 200) {
- InputStream inputStream = conn.getInputStream();
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while ((len = inputStream.read(buffer)) != -1) {
- outStream.write(buffer, 0, len);
- }
- String htmlStr = new String(outStream.toByteArray(), aEncode);
- inputStream.close();
- outStream.close();
- return htmlStr;
- }
- return null;
- }
希望本文所述对大家 Android 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0819/337578.html