黎活明8天快速掌握android视频教程--24_网络通信之网页源码查看器

1 该项目的主要功能就是从将后台的html网页在Android的界面上显示出来

后台就是建立一个java web工程在工程尚建立一个html或者jsp文件就可以了,这里主要看Android客户端的程序

xml文件:

复制代码
<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="application.weiyuan.com.lihuoming_24.MainActivity">

    <TextView
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="从网页获得html源码的显示" />

    <Button
        android:id="@+id/btn_main_download"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击下载"/>

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_main_html"
            android:textSize="25sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </ScrollView>


</LinearLayout>
复制代码

上面需要注意的是采用ScrollView包裹TextView,因为后台返回的html的内容很多,可以让TextView滚动起来

业务类的操作代码是:

复制代码
public class HtmlBussiess {
    public static String getHtml(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
        openConnection.setConnectTimeout(5000);
        openConnection.setRequestMethod("GET"); //采用get的请求方式
        openConnection.connect();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        InputStream inputStream = null;
        if(openConnection.getResponseCode() == 200){
            inputStream = openConnection.getInputStream();
            byte[] buffer = new byte[1024];

            int len = -1;
            while ((len = inputStream.read(buffer)) != -1){
                outputStream.write(buffer,0,len);
            }
        }
        inputStream.close();
        return  new String(outputStream.toByteArray(),"utf-8");//这里的编码方式必须和后台的html编码方式一样
    }
}
复制代码

activity控制层的代码是:

复制代码
public class MainActivity extends Activity {

    private TextView tv_main_html;
    private Button btn_main_download;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
    }

    private void initListener() {
            btn_main_download.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ExecutorService executorService = Executors.newCachedThreadPool();
                    executorService.execute(new Runnable() {
                        @Override
                        public void run() {
                            String path = "http://192.168.1.103:8080/lihuoming_23/hello.jsp";//myeclpise建立的工程
                            try {
                                final String html = HtmlBussiess.getHtml(path);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                       tv_main_html.setText(html);
                                    }
                                });

                            } catch (final Exception e) {
                                e.printStackTrace();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(MainActivity.this, "图片下载失败" + e.toString(),
                                                Toast.LENGTH_LONG).show();
                                    }
                                });
                                ;
                            }
                        }
                    });


                }
            });
        }


    private void initView() {
        tv_main_html = (TextView) findViewById(R.id.tv_main_html);
        btn_main_download = (Button) findViewById(R.id.btn_main_download);
    }
}
复制代码

 

posted on   luzhouxiaoshuai  阅读(157)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示