本文实现一个基于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查看器的更多相关文章

  1. 无废话Android之内容观察者ContentObserver、获取和保存系统的联系人信息、网络图片查看器、网络html查看器、使用异步框架Android-Async-Http(4)

    1.内容观察者ContentObserver 如果ContentProvider的访问者需要知道ContentProvider中的数据发生了变化,可以在ContentProvider 发生数据变化时调 ...

  2. Android -- 网络图片查看器,网络html查看器, 消息机制, 消息队列,线程间通讯

    1. 原理图 2. 示例代码 (网络图片查看器) (1)  HttpURLConnection (2) SmartImageView (开源框架:https://github.com/loopj/an ...

  3. 网络延迟查看器 Network latency view 1.4

    这是个用于查看网络延迟/ip/主机/地区的工具,内外网通吃,外网可通过这里下载csv以显示国家(地区) 可以自己决定winpcap或者原始套接字进行捕捉 如果只扫描内网推荐angryip 这是款发布在 ...

  4. 网络html查看器

    1)演示效果:

  5. Android项目——网络图片查看器

    效果-=-------------->加入包 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an ...

  6. Android smartimageview网络图片查看器

    调用代码: SmartImageView siv = (SmartImageView) findViewById(R.id.siv);siv.setImageUrl(et_path.getText() ...

  7. android 网络_网络图片查看器

    xml <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...

  8. android 网络_网络源码查看器

    xml设计 <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...

  9. Android 网络图片查看器

    今天来实现一下android下的一款简单的网络图片查看器 界面如下: 代码如下: <LinearLayout xmlns:android="http://schemas.android ...

随机推荐

  1. 用Latex写学术论文:作者(Author)&摘要(Abstract)

    标题&作者 1.标题 \title{} "Line breaks (\\) may be used to equalize the length of the title lines ...

  2. 做一个会PS切图的前端开发

    系列链接 做一个会使用PS的前端开发 做一个会PS切图的前端开发 切图方法分类 PhotoShop从CS版本演变到现在的CC版本,切图功能发生了比较大的变化,我们可以把PhotoShop CS版本时的 ...

  3. Android Studio导入项目非常慢的解决办法

    问题 Android Studio目前已经更新到2.0 Preview 6了,作为Google大力推崇的开发工具,相对于Eclipse ADT有着不可比拟的优势.然而在实际使用时,依然有不少不爽的地方 ...

  4. 30天C#基础巩固------this,base,string中的方法,StringBuilder性能

    这里主要是记录下自己学习笔记,希望有个地方在以后可以看到自己走过的路. 关于之前多态的知识有一个口诀,很好理解里面的override和new,virtual关键字. "new则隐藏,over ...

  5. 矩阵乘法 --- hdu 4920 : Matrix multiplication

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  6. javascript事件分类解析

    最近在学习javascript,就顺便把常用事件给大家整理整理,也让自己加深印象.不足之处欢迎各位补充. 一般事件 onclick 鼠标点击时触发此事件 ondblclick  鼠标双击时触发此事件 ...

  7. ASP.Net中防止页面刷新重复提交的几种方法

    [摘要] 目前很多网站都要提交页面插入或更新数据库,比如留言本,一个用户提交留言后,如果按F5,就会重新提交一遍留言,导致数据库出现两条一模一样的留言,本文介绍了几种防止页面刷新,导致重复提交数据的方 ...

  8. MVC之前的那点事儿系列

    MVC之前的那点事儿系列,是笔者在2012年初阅读MVC3源码的时候整理的,主要讲述的是从HTTP请求道进入MVCHandler之前的内容,包括了原创,翻译,转载,整理等各类型文章,当然也参考了博客园 ...

  9. The Amazing ProgressBar Control(转)

    好久没写博客了,今天就先转一篇,随后可以再写些~~~ 直接把原文粘过来,就不再进行翻译和个人说明了,因为效果很COOL~ The Amazing ProgressBar Control A progr ...

  10. Android实现系统重新启动

    有些Android版本没有系统重启的功能,非常不方便.需要我们自己开发一个能够重新启动的应用. 首先定义布局文件: <?xml version="1.0" encoding= ...