Android 网络HTML查看器
本文实现一个基于Android的网络HTML查看器
新建项目,项目布局文件如下:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" > <EditText
android:id="@+id/et_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入html路径" /> <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="查看" /> <ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <TextView
android:id="@+id/tv_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TextView>
</ScrollView> </LinearLayout>

新建工具类StreamTools.java:
package com.wuyudong.htmlviewer.utils; import java.io.ByteArrayOutputStream;
import java.io.InputStream; public class StreamTools {
/**
* 把输入流的内容转化成字符串
*
* @param is
* @return
*/
public static String readInputStream(InputStream is) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
is.close();
baos.close();
byte[] result = baos.toByteArray();
return new String(result);
} catch (Exception e) {
e.printStackTrace();
return null;
} } }
完整代码如下:
package com.wuyudong.htmlviewer; import java.io.InputStream;
import java.io.StreamCorruptedException;
import java.net.HttpURLConnection;
import java.net.URL; import com.wuyudong.htmlviewer.utils.StreamTools; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { protected static final int ERROR = 1;
protected static final int SHOW_TEXT = 2; private TextView tv_content;
private EditText et_path; // 定义一个消息处理器
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case ERROR:
Toast.makeText(MainActivity.this, "获取数据失败", 0).show();
break;
case SHOW_TEXT:
String text = (String) msg.obj;
tv_content.setText(text);
break;
}
};
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tv_content = (TextView) findViewById(R.id.tv_content);
et_path = (EditText) findViewById(R.id.et_path); } public void click(View view) {
final String path = et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(this, "路径不能为空", 0).show();
} else {
new Thread() {
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"); int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
String result = StreamTools.readInputStream(is);
// tv_content.setText(result);
Message msg = new Message();
msg.what = SHOW_TEXT;
msg.obj = result;
handler.sendMessage(msg);
} else {
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg); } };
}.start(); } } }
Android 网络HTML查看器的更多相关文章
- 无废话Android之内容观察者ContentObserver、获取和保存系统的联系人信息、网络图片查看器、网络html查看器、使用异步框架Android-Async-Http(4)
1.内容观察者ContentObserver 如果ContentProvider的访问者需要知道ContentProvider中的数据发生了变化,可以在ContentProvider 发生数据变化时调 ...
- Android -- 网络图片查看器,网络html查看器, 消息机制, 消息队列,线程间通讯
1. 原理图 2. 示例代码 (网络图片查看器) (1) HttpURLConnection (2) SmartImageView (开源框架:https://github.com/loopj/an ...
- 网络延迟查看器 Network latency view 1.4
这是个用于查看网络延迟/ip/主机/地区的工具,内外网通吃,外网可通过这里下载csv以显示国家(地区) 可以自己决定winpcap或者原始套接字进行捕捉 如果只扫描内网推荐angryip 这是款发布在 ...
- 网络html查看器
1)演示效果:
- Android项目——网络图片查看器
效果-=-------------->加入包 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an ...
- Android smartimageview网络图片查看器
调用代码: SmartImageView siv = (SmartImageView) findViewById(R.id.siv);siv.setImageUrl(et_path.getText() ...
- android 网络_网络图片查看器
xml <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...
- android 网络_网络源码查看器
xml设计 <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...
- Android 网络图片查看器
今天来实现一下android下的一款简单的网络图片查看器 界面如下: 代码如下: <LinearLayout xmlns:android="http://schemas.android ...
随机推荐
- Django之路由、模板和模型系统
一.路由系统 浏览器会自动给url后加一个"/" django会自动给路由的正则表达式前面加一个"/" django会给任何不带"/"结尾的 ...
- Windows Server 2012安装时所需要的KEY
Windows Server 2012不像Server 2008和2008 R2那样可以先装系统再输入序列号,而是在一开始就必须输入Server 2012 cdkey,目前在网上找到两枚序列号,标准版 ...
- 两种读写配置文件的方案(app.config与web.config通用)
第一种方法:采用MS现有的ConfigurationManager来进行读写 using System.Configuration; namespace Zwj.TEMS.Common { publi ...
- Elasticsearch 教程--入门
1.1 初识 Elasticsearch 是一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎,可以说 Lucene 是当今最先进,最高效的全功能开源搜索引擎框架. 但是 L ...
- C#基础03
学习集合的一些知识.集合:泛型集合,非泛型集合;ArrayList,Hashtable,List<T>,Dictionary<k,v>等,还有一些集合的常用方法. 一:集合的介 ...
- 基于MDK-ARM创建STM32L-DISCOVERY Project
本文只针对使用MDK-ARM建立软件开发环境,并基于STM32L1xx_StdPeriph_Lib_V1.1.1库及其Examples,其余情况可参考UM1451 User manual Gettin ...
- HoverTree项目已经实现分层
HoverTree项目已经初步实现分层,源代码已经上传到 http://hovertree.com/down/ 请到SOURCE CODE查看. 在本地用SQL Server 2008 数据库测试成功 ...
- .net微信公众号开发——消息与事件
作者:王先荣 本文介绍如何处理微信公众号开发中的消息与事件,包括:(1)消息(事件)概况:(2)验证消息的真实性:(3)解析消息:(4)被动回复消息:(5)发送其他消息. 开源项目地址:h ...
- python sqlite中通过字段名获取查询结果
在连sqlite数据库时,用fetchall()查询结果,是用row[0],row[1]这样的方式来打印每列的结果 但是我想用row[“字段名”]方式查询怎么办? import sqlite3 con ...
- 背水一战 Windows 10 (7) - 控件 UI: VisualState, VisualStateManager, 控件的默认 UI
[源码下载] 背水一战 Windows 10 (7) - 控件 UI: VisualState, VisualStateManager, 控件的默认 UI 作者:webabcd 介绍背水一战 Wind ...