文件异步下载

1. MainActivity

public class MainActivity extends AppCompatActivity {

    private TextView show;
    private Button mbut;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = (TextView)findViewById(R.id.show);
        mbut = (Button)findViewById(R.id.mbut);
        mbut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    download(v);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void download(View Source) throws MalformedURLException {
        DownTask task = new DownTask(this);
        task.execute(new URL("http://www.crazyit.org/ethos.php"));
    }

    private class DownTask extends AsyncTask<URL,Integer,String>{
        //可变长的输入参数,与AsyncTask.exucute()对应
        ProgressDialog pdialog;
        //定义已经记录读取的行数
        int hasRead = 0;
        Context mContext;
        public DownTask(Context ctx) {
            mContext = ctx;
        }

        @Override
        protected String doInBackground(URL... params) {
            StringBuilder sb = new StringBuilder();
            try {
                //打开conn连接对应的输入流,并将它包装成BufferedReader
                URLConnection conn = params[0].openConnection();
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),
                        "utf-8"));
                String line = null;
                while((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                    hasRead ++;
                    publishProgress(hasRead);
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            //返回HTML页面内容
            show.setText(s);
            pdialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            pdialog = new ProgressDialog(mContext);
            pdialog.setTitle("任务正在执行中...");
            pdialog.setMessage("任务正在执行中,请稍等...");
            //不能使用取消按钮
            pdialog.setCancelable(false);
            //设置进度条的最大进度值
            pdialog.setMax(202);
            pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            //设置对话框的进度条是否显示进度
            pdialog.setIndeterminate(false);
            pdialog.show();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            show.setText("已经读取了【"+values[0]+"】行!");
            pdialog.setProgress(values[0]);
        }
    }
  }

2.本程序需要访问网络资源,因此还需要在AndroidMainfest.xml文件中声明以下权限:

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


你可能感兴趣的:(文件异步下载)