Android带进度条的文件上传 使用AsyncTask异步任务 Android带进度条的文件上传示例(使用AsyncTask异步任务)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android带进度条的文件上传 使用AsyncTask异步任务 Android带进度条的文件上传示例(使用AsyncTask异步任务)

yx0628   2021-03-22 我要评论
想了解Android带进度条的文件上传示例(使用AsyncTask异步任务)的相关内容吗,yx0628在本文为您仔细讲解Android带进度条的文件上传 使用AsyncTask异步任务的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:asynctask异步任务,asynctask进度条,asynctask更新进度条,下面大家一起来学习吧。

最近项目中要做一个带进度条的上传文件的功能,学习了AsyncTask,使用起来比较方便,将几个方法实现就行,另外做了一个很简单的demo,希望能对大家有帮助,在程序中设好文件路径和服务器IP即可。

demo运行截图:

AsyncTask是抽象类,子类必须实现抽象方法doInBackground(Params... p),在此方法中实现任务的执行工作,比如联网下载或上传。AsyncTask定义了三种泛型类型Params,Progress和Result。

1、Params 启动任务执行的输入参数,比如HTTP请求的URL,上传文件的路径等;

2、Progress 后台任务执行的百分比;

3、Result 后台执行任务的最终返回结果,比如String。

AsyncTask 的执行分为四个步骤,与前面定义的TaskListener类似。每一步都对应一个回调方法,需要注意的是这些方法不应该由应用程序调用,开发者需要做的就是实现这些方法。在任务的执行过程中,这些方法被自动调用。

1、onPreExecute(), 该方法将在执行实际的后台操作前被UI thread调用。可以在该方法中做一些准备工作,如在界面上显示一个进度条。

2、doInBackground(Params...), 将在onPreExecute 方法执行后马上执行,该方法运行在后台线程中。这里将主要负责执行那些很耗时的后台计算工作。可以调用 publishProgress方法来更新实时的任务进度。该方法是抽象方法,子类必须实现。

3、onProgressUpdate(Progress...),在publishProgress方法被调用后,UI thread将调用这个方法从而在界面上展示任务的进展情况,例如通过一个进度条进行展示。

4、onPostExecute(Result), 在doInBackground 执行完成后,onPostExecute 方法将被UI thread调用,后台的计算结果将通过该方法传递到UI thread.

主进程中使用下面两行开始异步任务:

mTask = new MyTask(); 
mTask.execute(filePath, url); 

doInBackground()函数中,params[0]和params[1]本别对应execute()的第一个和第二个变量。

private class MyTask extends AsyncTask<String, Integer, String>{ 
 
    @Override 
    protected void onPostExecute(String result) { 
      //最终结果的显示 
      mTvProgress.setText(result);   
    } 
 
    @Override 
    protected void onPreExecute() { 
      //开始前的准备工作 
      mTvProgress.setText("loading..."); 
    } 
 
    @Override 
    protected void onProgressUpdate(Integer... values) { 
      //显示进度 
      mPgBar.setProgress(values[0]); 
      mTvProgress.setText("loading..." + values[0] + "%"); 
    } 
 
    @Override 
    protected String doInBackground(String... params) { 
      //这里params[0]和params[1]是execute传入的两个参数 
      String filePath = params[0]; 
      String uploadUrl = params[1]; 
      //下面即手机端上传文件的代码 
      String end = "\r\n"; 
      String twoHyphens = "--"; 
      String boundary = "******"; 
      try { 
        URL url = new URL(uploadUrl); 
        HttpURLConnection httpURLConnection = (HttpURLConnection) url 
            .openConnection(); 
        httpURLConnection.setDoInput(true); 
        httpURLConnection.setDoOutput(true); 
        httpURLConnection.setUseCaches(false); 
        httpURLConnection.setRequestMethod("POST"); 
        httpURLConnection.setConnectTimeout(6*1000); 
        httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); 
        httpURLConnection.setRequestProperty("Charset", "UTF-8"); 
        httpURLConnection.setRequestProperty("Content-Type", 
            "multipart/form-data;boundary=" + boundary); 
 
        DataOutputStream dos = new DataOutputStream(httpURLConnection 
            .getOutputStream()); 
        dos.writeBytes(twoHyphens + boundary + end); 
        dos 
            .writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" 
                + filePath.substring(filePath.lastIndexOf("/") + 1) 
                + "\"" + end); 
        dos.writeBytes(end); 
 
        //获取文件总大小 
        FileInputStream fis = new FileInputStream(filePath); 
        long total = fis.available(); 
        byte[] buffer = new byte[8192]; // 8k 
        int count = 0; 
        int length = 0; 
        while ((count = fis.read(buffer)) != -1) { 
          dos.write(buffer, 0, count); 
          //获取进度,调用publishProgress() 
          length += count; 
          publishProgress((int) ((length / (float) total) * 100)); 
          //这里是测试时为了演示进度,休眠500毫秒,正常应去掉 
          Thread.sleep(500); 
        }     
        fis.close(); 
        dos.writeBytes(end); 
        dos.writeBytes(twoHyphens + boundary + twoHyphens + end); 
        dos.flush(); 
 
        InputStream is = httpURLConnection.getInputStream(); 
        InputStreamReader isr = new InputStreamReader(is, "utf-8"); 
        BufferedReader br = new BufferedReader(isr); 
        @SuppressWarnings("unused") 
        String result = br.readLine(); 
        dos.close(); 
        is.close(); 
        return "上传成功"; 
    }catch (Exception e) { 
      e.printStackTrace(); 
      return "上传失败"; 
    }   
  } 

界面中只要一个进度条progressBar 和一个用于显示的TextView即可。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们