編寫一個Android程序,使用AsyncTask實現獲取網頁的Html代碼,並且使用TextView顯示。

編寫一個Android程序,使用AsyncTask實現獲取網頁的Html代碼,並且使用TextView顯示。

MainActivity.java代碼:

package com.example.administrator.chapter_listener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
class MainActivity extends AppCompatActivity{
    private TextView text;
    private ProgressBar mProgressBar=null;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text=(TextView)findViewById(R.id.textView);
        btn=(Button)findViewById(R.id.button4);
        //爲按鈕綁定事件響應方法
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                try {
                    download();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }
    //重寫該方法,爲界面的按鈕提供事件響應方法
    public void download() throws  MalformedURLException{
        CodeTask task=new CodeTask();
        task.execute(new URL("https://www.baidu.com/"));
    }
    class CodeTask extends AsyncTask<URL,Integer,String>{
        //完成實際的下載任務
        protected String doInBackground(URL... params){
            StringBuilder sb=new StringBuilder();
            try{
                URLConnection conn=params[0].openConnection();
                //打開conn連接對應的輸入流,並把它包裝成BufferedReader
                BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
                String line=null;
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected  void  onPreExecute(){
            mProgressBar.setVisibility(View.VISIBLE);
        }
        protected  void onPostExecute(String result){
            mProgressBar.setVisibility(View.GONE);
            text.setText(result);
        }
    }
}

activity_main.xml佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.chapter_listener.MainActivity">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="神奇文本" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="開始操作"
        android:layout_marginTop="52dp"
        android:layout_below="@+id/progressBar1"
        android:layout_alignLeft="@+id/progressBar1"
        android:layout_alignStart="@+id/progressBar1" />


</RelativeLayout>

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章