解析html
2015-12-17 09:00
537 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/l__yx/article/details/50337153
package com.xh.tx.html; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { TextView tv_html_content = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_html_content = (TextView) findViewById(R.id.tv_html_content); loadHtml("http://10.0.2.2:8080/baidu/"); } //加载html public void loadHtml(String uri) { try { final URL url = new URL(uri); new Thread(new Runnable() { @Override public void run() { HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setRequestMethod("GET");//以get的方式去请求数据,如果没有这句话,默认就是以get方式请求 conn.connect(); //连接 /** * 网络连接是否一定成功? 》》 否定的 * 如何判断网络一定成功 >> 在http请求中有一个状态来标示网络是否成功 */ if(conn.getResponseCode() == 200) { final String content = releaseInputStream(conn.getInputStream()); //非要打印一句话 runOnUiThread(new Runnable() { @Override public void run() { if(null == content) { Toast.makeText(MainActivity.this, "加载网页源代码失败", 0).show(); }else { tv_html_content.setText(content); } } }); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(null != conn) { conn.disconnect(); } } } }).start(); } catch (MalformedURLException e1) { e1.printStackTrace(); } } public String releaseInputStream(InputStream in) throws IOException { byte[] buffer = new byte[1024]; ByteArrayOutputStream bytearray = new ByteArrayOutputStream(); int len = 0; if(null == in) { return null; }else { //正常解析 while((len = in.read(buffer, 0, 1024)) != -1) { bytearray.write(buffer); } String content = bytearray.toString(); if(content.indexOf("GBK") != -1 || content.indexOf("gbk") != -1) { content = new String(bytearray.toByteArray(),"GBK"); } bytearray.close(); return content; } } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.xh.tx.html.MainActivity" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_html_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </ScrollView> </RelativeLayout>
相关文章推荐
- HTML+CSS章节要点解析
- jsoup解析html 用法
- android中jsoup解析html的几个例子
- C# 解析HTML更简单了
- 类jquery的php解析html库,simple_html_dom
- JS解析xml显示在html
- Html中代码换行造成空格间距的问题解析
- C#中利用Markup Service实现HTML解析为DOM Tree
- 使用v-html解决Vue.js渲染过程中html标签不能被解析(html标签显示为字符串)
- C#中使用HtmlAgilityPack对html进行解析
- Python进阶 - HTML获取与解析
- 玩转PHP字符串变量解析和函数,动态输出HTML
- Jsoup-HTML解析
- 关于Apache不能解析html内容
- iOS解析HTML
- C#解析html文档
- 如何使用Objective-C解析HTML和XML
- php解析html类(2)
- 在 Android 应用中使用 HTML Parser 便捷的解析 html 内容
- nutch对某个contentType文档进行解析的流程--html