星空网 > 软件开发 > 操作系统

【Android Demo】获取指定网页的页面源代码

1.直接上效果图

【Android Demo】获取指定网页的页面源代码images/loading.gif' data-original="http://images.cnitblog.com/blog/359646/201412/301622349506189.png" />

2.代码

主要就是工具类HtmlService.java:

import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;/** * 获取HTML数据 * * @author David * */public class HtmlService {  public static String getHtml(String path) throws Exception {    // 通过网络地址创建URL对象    URL url = new URL(path);    // 根据URL    // 打开连接,URL.openConnection函数会根据URL的类型,返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection    HttpURLConnection conn = (HttpURLConnection) url.openConnection();    // 设定URL的请求类别,有POST、GET 两类    conn.setRequestMethod("GET");    //设置从主机读取数据超时(单位:毫秒)    conn.setConnectTimeout(5000);    //设置连接主机超时(单位:毫秒)    conn.setReadTimeout(5000);    // 通过打开的连接读取的输入流,获取html数据    InputStream inStream = conn.getInputStream();    // 得到html的二进制数据    byte[] data = readInputStream(inStream);    // 是用指定的字符集解码指定的字节数组构造一个新的字符串    String html = new String(data, "utf-8");    return html;  }  /**   * 读取输入流,得到html的二进制数据   *   * @param inStream   * @return   * @throws Exception   */  public static byte[] readInputStream(InputStream inStream) throws Exception {    ByteArrayOutputStream outStream = new ByteArrayOutputStream();    byte[] buffer = new byte[1024];    int len = 0;    while ((len = inStream.read(buffer)) != -1) {      outStream.write(buffer, 0, len);    }    inStream.close();    return outStream.toByteArray();  }}

MainActivity.java 修改如下:

public class MainActivity extends Activity {  private String path = "http://www.cnblogs.com/yc-755909659/";  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);        TextView textView = (TextView)this.findViewById(R.id.textView);     try {       String htmlContent = HtmlService.getHtml(path);      textView.setText(htmlContent);     } catch (Exception e) {         textView.setText("程序出现异常:"+e.toString());    }   }}

activity_main.

<ScrollView ="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent" >  <TextView    android:id="@+id/textView"    android:layout_width="wrap_content"    android:layout_height="wrap_content" /></ScrollView>

最后,记得添加网络访问权限哦

<uses-permission android:name="android.permission.INTE.NET"/>

 

来源:Android获取HTML数据






原标题:【Android Demo】获取指定网页的页面源代码

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Wish调整多国路向物流价格并同步提升物流服务水平:https://www.ikjzd.com/articles/96137
儿童节选品:这些产品最好别卖!:https://www.ikjzd.com/articles/96138
从Forever 21退出中国看快销品牌们的生死劫:https://www.ikjzd.com/articles/96139
分手29天后,跨境小伙创业被坑30万,但结局却...:https://www.ikjzd.com/articles/96141
eBay中的Selling Manager 和 Selling Manager Pro傻傻分不清?:https://www.ikjzd.com/articles/96142
亚马逊精品如何打造?:https://www.ikjzd.com/articles/96143
温州旧货市场有玻璃柜卖吗?:https://www.vstour.cn/a/411246.html
如何用摄影作品表现“芳草鲜美,落英缤纷”的:https://www.vstour.cn/a/411247.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流