zoukankan      html  css  js  c++  java
  • asynctask 异步下载

    public class MainActivity extends Activity
    {
    private TextView show;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    show = (TextView) findViewById(R.id.show);
    }
    // 重写该方法,为界面的按钮提供事件响应方法
    public void download(View source) throws MalformedURLException
    {
    DownTask task = new DownTask(this);
    task.execute(new URL("http://www.crazyit.org/ethos.php"));
    }
    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
    {
    URLConnection conn = params[0].openConnection();
    // 打开conn连接对应的输入流,并将它包装成BufferedReader
    BufferedReader br = new BufferedReader(
    new InputStreamReader(conn.getInputStream()
    , "utf-8"));
    String line = null;
    while ((line = br.readLine()) != null)
    {
    sb.append(line + " ");
    hasRead++;
    publishProgress(hasRead);
    }
    return sb.toString();
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    return null;
    }
    @Override
    protected void onPostExecute(String result)
    {
    // 返回HTML页面的内容
    show.setText(result);
    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]);
    }
    }
    }

  • 相关阅读:
    C#Redis缓存帮助类
    [RxSwift教程]9、过滤操作符:filter、take、skip等
    [RxSwift教程]8、变换操作符:buffer、map、flatMap、scan等
    [RxSwift教程]7、Subjects、Variables
    [RxSwift教程]6、观察者2: 自定义可绑定属性
    [RxSwift教程]5、观察者1: AnyObserver、Binder
    [RxSwift教程]4、Observable订阅、事件监听、订阅销毁
    [RxSwift教程]3、Observable介绍、创建可观察序列
    [RxSwift教程]2、响应式编程与传统式编程的比较样例
    [RxSwift教程]1、安装、介绍
  • 原文地址:https://www.cnblogs.com/ouyangping/p/7440016.html
Copyright © 2011-2022 走看看