制作html源文件查看器

来源:互联网 发布:软件需求工程师转行 编辑:程序博客网 时间:2024/06/05 20:22

在前面的博客中介绍了网络图片查看器,这篇博客继续介绍Android中的网络编程,这篇博客介绍在Android中读取从服务器端发送过来的htm文件

第一步:启动Tomcat服务器,并将一个html文件部署到服务器中

第二步:使用Android Studio创建一个Android工程,修改activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?><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"    tools:context="com.fyt.htmlview.MainActivity"    android:orientation="vertical">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="查看html文件"        android:onClick="Clicked"/>    <ScrollView        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:id="@+id/tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"/>    </ScrollView></LinearLayout>


第三步:新建一个Util.java文件,并在文件中添加下面的代码

package com.fyt.htmlview;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;//创建一个通用类public class Util {    //从流中获得文本    public static String getTextFromStream(InputStream is){        //创建字节数组        byte[] b = new byte[1024];        //创建整型变量len,用于记录文本的长度        int len = 0;        //创建字节数组输出流,读取输入流的文本数据时,同步把数据写入数组输出流        ByteArrayOutputStream bos = new ByteArrayOutputStream();        try {            //将读取到的数据写入字节数组输出流            while((len = is.read(b)) != -1)            {                bos.write(b, 0, len);            }            //把字节数组输出流里的数据转换成字符串            String text = new String(bos.toByteArray());            //返回字符串            return text;        } catch (IOException e) {            e.printStackTrace();        }        //当读取出现异常,则返回null        return null;    }}


第四步:修改MainActivity.java文件

package com.fyt.htmlview;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.View;import android.widget.TextView;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    Handler handler = new Handler()    {        public void handleMessage(android.os.Message msg)        {            TextView tv = (TextView) findViewById(R.id.tv);            tv.setText((String)msg.obj);        }    };    //查看html文件响应函数    public void Clicked(View v)    {        //创建子线程        Thread t = new Thread() {            //执行子线程            @Override            public void run()            {                //设置html文件的地址                String path = "http://192.168.1.101:8080/app/baidu.html";                try {                    //将地址封装成URL对象                    URL url = new URL(path);                    //获取连接对象,此时还未建立连接                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();                    //设置请求方式为Get请求                    conn.setRequestMethod("GET");                    //设置连接超时                    conn.setConnectTimeout(5000);                    //设置读取超时                    conn.setReadTimeout(5000);                    //先建立连接,然后获取响应码                    //如果连接成功                    if(conn.getResponseCode() == 200)                    {                        //拿到服务器返回的输入流,流里的数据就是html的源文件                        InputStream is = conn.getInputStream();                        //从流里把文本数据取出来                        String text = Util.getTextFromStream(is);                        //创建消息对象                        Message msg = handler.obtainMessage();                        //设置消息对象携带的数据                        msg.obj = text;                        //将消息发送到主线程                        handler.sendMessage(msg);                    }                } catch (Exception e) {                    e.printStackTrace();                }            }        };        //启动子线程        t.start();    }}

最后在配置文件中添加一条访问网络的权限

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

程序的运行效果


0 0
原创粉丝点击