码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-04-24 12:06:39      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:view   rri   请求方式   工程   runnable   apk   需要   state   ble   

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);
    }
}

 

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

标签:view   rri   请求方式   工程   runnable   apk   需要   state   ble   

原文地址:http://www.cnblogs.com/kebibuluan/p/6755534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!