Android带进度条的文件上传示例(使用AsyncTask异步任务)
Android带进度条的文件上传示例(使用AsyncTask异步任务)
发布时间:2016-12-28 来源:查字典编辑
摘要:最近项目中要做一个带进度条的上传文件的功能,学习了AsyncTask,使用起来比较方便,将几个方法实现就行,另外做了一个很简单的demo,希...

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

demo运行截图:

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

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

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 = "rn"; 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即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持查字典教程网。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新安卓软件开发学习
热门安卓软件开发学习
编程开发子分类