黎活明8天快速掌握android視頻教程--24_網絡通訊之網頁源碼查看器

1 該項目的主要功能就是從將後臺的html網頁在Android的界面上顯示出來html

後臺就是創建一個java web工程在工程尚創建一個html或者jsp文件就能夠了,這裏主要看Android客戶端的程序java

xml文件:android

<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滾動起來web

業務類的操做代碼是:app

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控制層的代碼是:jsp

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);
    }
}
相關文章
相關標籤/搜索