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 ...
随机推荐
- Redis发布订阅实现原理
发布订阅中使用到的命令就只有三个:PUBLISH,SUBSCRIBE,PSUBSCRIBE PUBLISH 用于发布消息 SUBSCRIBE 也叫频道订阅,用于订阅某一特定的频道 PSUBSCRIBE ...
- 关于WEB Service&WCF&WebApi实现身份验证之WEB Service篇
在这个WEB API横行的时代,讲WEB Service技术却实显得有些过时了,过时的技术并不代表无用武之地,有些地方也还是可以继续用他的,我之所以会讲解WEB Service,源于我最近面试时被问到 ...
- 实现UniqueAttribute唯一性约束-优化版
我之前就写过一篇<实现UniqueAttribute唯一性约束>,虽然实现了通过调用IsValid方法可以进行唯一性验证,但有一个缺点,那就是耦合度过高,原因是里面的DB上下文对象是直接写 ...
- [python基础]关于中文编码和解码那点事儿
我们在用python处理中文的时候,或多或少会遇到这样一些错误 常见错误1: SyntaxError: Non-ASCII character '\xe4' in file C 常见错误2: Unic ...
- Maven提高篇系列之(四)——使用Profile
这是一个Maven提高篇的系列,包含有以下文章: Maven提高篇系列之(一)——多模块 vs 继承 Maven提高篇系列之(二)——配置Plugin到某个Phase(以Selenium集成测试为例) ...
- 问得最多的十个JavaScript前端面试问题
我知道有很多人不同意这种类型的面试.其实不管你喜不喜欢,你都得接受.尤其当你是自学的,而且要申请第一份工作时. 我估计很多有人其它方法来证明他自己,像Github/ 项目地址可能是非常理想的证明方 ...
- SQL--Order By
Order By--将当前查询出来的数据结果,进行排序,排序按照后面指定的列排序; DESC--倒序排序(降序); ASC--正序排序(升序); 汉字:用拼音的首字母排序,升序是从a--z;降序是从z ...
- LeetCode - 51. N-Queens
51. N-Queens Problem's Link ------------------------------------------------------------------------ ...
- 策略模式(Stategy Pattern)
知识点 找出引用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起. 针对接口编程,而不是实现编程. 多用组合,少用继承 示例
- 微信--获取access_token
今天,终于鼓足勇气,来到这片圣地,迎来人生新的开始. 第一次...... 最近做微信公众号,记录一下,仅供参看. 关于access_token微信公众号有相关说明: access_token是公众号的 ...