• android开发步步为营之67:使用android开源项目android-async-http异步下载文件


             android-async-http项目地址 https://github.com/loopj/android-async-http。android-async-http顾名思义是异步的http请求,只是它也支持同步请求的,本文主要使用了android-async-http做异步请求下载文件。每当app须要更新新版本号的时候。就须要用到下载功能的,经研究android-async-http这个第三方开源项目还是挺好用的,这里介绍给大家。

            1、下载类DownloadManager.java

    package com.figo.study.latin.mgr;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    
    import org.apache.http.Header;
    
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.os.Environment;
    
    import com.figo.study.R;
    import com.android.inputmethod.latin.model.AsyncDownload;
    import com.figo.study.util.FileUtil;
    import com.figo.study.util.NetworkUtil;
    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.FileAsyncHttpResponseHandler;
    
    /**
     * 异步下载类
     * @author figo
     *
     */
    public class DownloadManager {
        private boolean mUseWifiOnly = false;
    
        public DownloadManager() {
    
        }
    
        private HashMap<String, AsyncDownload> asyncDownlaods = null;
    
        public void init() {
            asyncDownlaods = new HashMap<String, AsyncDownload>();
        }
    
        public void download(final Context context, String url, final String md5, String dirPath, String fileName, final DownloadListener callback) {
            try {
                callback.onStart();
                if (!NetworkUtil.isNetworkUseful(context)) {
                    callback.onError(context.getString(R.string.lbl_network_useless));
                }
                if (mUseWifiOnly) {
                    if (NetworkUtil.getNetworkType(context) != ConnectivityManager.TYPE_WIFI) {
                        callback.onError(context.getString(R.string.lbl_not_support));
                    }
                    return;
                }
                String sdPath = Environment.getExternalStorageDirectory() + "/";
                AsyncHttpClient client =new AsyncHttpClient();
                if (!asyncDownlaods.containsKey(url)) {
                    AsyncDownload asyncDownload = new AsyncDownload();
                    asyncDownload.setAsyncHttpClient(client);
                    asyncDownload.setFileName(sdPath + dirPath + fileName);
                    asyncDownlaods.put(url, asyncDownload);
                }else
                {
                    client=asyncDownlaods.get(url).getAsyncHttpClient();
                }
    
                File file = new File(sdPath + dirPath, fileName);
                try {
                    FileUtil.createSDDir(dirPath);
                    file = FileUtil.createSDFile(dirPath + fileName);
                } catch (IOException e1) {
                    if (e1 != null) {
                        e1.printStackTrace();
                    }
                }
                FileAsyncHttpResponseHandler fileAsyncHttpResponseHandler = new FileAsyncHttpResponseHandler(file) {
    
                    @Override
                    public void onCancel() {
                        super.onCancel();
                        System.out.print("cancel success!");
                    }
    
                    @Override
                    public void onProgress(int bytesWritten, int totalSize) {
                        super.onProgress(bytesWritten, totalSize);
                        int currentProgress = (int) ((bytesWritten * 1.0f / totalSize) * 100);
                        callback.onProgress(currentProgress);
                    }
    
                    @Override
                    public void onStart() {
                        super.onStart();
    
                    }
    
                    @Override
                    public void onFailure(int arg0, Header[] arg1, Throwable arg2, File arg3) {
                        if (arg2 != null) {
                            arg2.printStackTrace();
                        }
                        if (arg2 != null) {
                            callback.onError(arg2.getMessage());
                        } else {
                            callback.onError(context.getString(R.string.lbl_network_error));
                        }
                    }
    
                    @Override
                    public void onSuccess(int arg0, Header[] arg1, File arg2) {
                        String md5String = "";
                        try {
                            md5String = FileUtil.getFileMD5String(arg2);
    
                        } catch (Exception e) {
                            if (e != null) {
                                e.printStackTrace();
                            }
                        }
    
    //                    if (md5.equals(md5String) == false) {
    //                        callback.onError(context.getString(R.string.lbl_md5_error));
    //                    } else {
    //                        callback.onSuccess();
    //                    }
                        //測试放开
                        callback.onSuccess();
    
                    }
    
                };
                client.setEnableRedirects(true);//同意反复下载
                client.get(context, url, fileAsyncHttpResponseHandler);
            } catch (Exception e) {
                callback.onError(context.getString(R.string.lbl_system_err));
            }
    
        }
    
        public void setDownloadWifiOnly(boolean value) {
            mUseWifiOnly = value;
    
        }
    
        public void cancelDownload(Context context, String url) {
            try {
    
                if (asyncDownlaods != null && asyncDownlaods.containsKey(url)) {
                    //已经下载的文件删除
                    FileUtil.deleteFile(asyncDownlaods.get(url).getFileName());
                    //取消当前下载
                    AsyncHttpClient client = asyncDownlaods.get(url).getAsyncHttpClient();
                    if (client != null) {
                        client.cancelRequests(context, true);
                    }
                    //当前key删除
                    asyncDownlaods.remove(url);
                }
    
            } catch (Exception e) {
                if (e != null) {
                    e.printStackTrace();
                }
            }
    
        }
    
    }
    
    

    2、下载监听类DownloadListener.java

    public interface DownloadListener {
        public void onStart();
    
        public void onError(String errMessage);
    
        public void onSuccess();
    
        public void onProgress(int progress);
    }


    3、文件管理类FileUtil.java

    /**
     * 
     */
    package com.figo.study.util;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    import android.os.Environment;
    
    /**
     * @author figo
     *
     */
    public class FileUtil {
         //得到当前外部存储设备的文件夹( /SDCARD )  
        static String SDPATH = Environment.getExternalStorageDirectory() + "/";
        /**
         * 默认的password字符串组合,用来将字节转换成 16 进制表示的字符,apache校验下载的文件的正确性用的就是默认的这个组合
         */
        protected static char hexDigits[] = {
                '0',
                '1',
                '2',
                '3',
                '4',
                '5',
                '6',
                '7',
                '8',
                '9',
                'a',
                'b',
                'c',
                'd',
                'e',
                'f' };
        protected static MessageDigest messagedigest = null;
        static {
            try {
                messagedigest = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
    
        public static String getFileMD5String(File file) throws IOException {
            InputStream fis;
            fis = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int numRead = 0;
            while ((numRead = fis.read(buffer)) > 0) {
                messagedigest.update(buffer, 0, numRead);
            }
            fis.close();
            return bufferToHex(messagedigest.digest());
        }
    
        private static String bufferToHex(byte bytes[]) {
            return bufferToHex(bytes, 0, bytes.length);
        }
    
        private static String bufferToHex(byte bytes[], int m, int n) {
            StringBuffer stringbuffer = new StringBuffer(2 * n);
            int k = m + n;
            for (int l = m; l < k; l++) {
                appendHexPair(bytes[l], stringbuffer);
            }
            return stringbuffer.toString();
        }
    
        private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
            char c0 = hexDigits[(bt & 0xf0) >> 4];// 取字节中高 4 位的数字转换
            // 为逻辑右移,将符号位一起右移,此处未发现两种符号有何不同
            char c1 = hexDigits[bt & 0xf];// 取字节中低 4 位的数字转换
            stringbuffer.append(c0);
            stringbuffer.append(c1);
        }
    
        public static void main(String[] args) throws IOException {
            File file = new File("E:/test/crm_account_YYYY_MM_DD.txt");
            String md5 = getFileMD5String(file);
            System.out.println("md5:" + md5);
        }
    
        /**  
         * 在SD卡上创建文件  
         * @param fileName  
         * @return  
         * @throws IOException  
         */
        public static File createSDFile(String fileName) throws IOException {
            File file = new File(SDPATH + fileName);
            if(file.exists())
            {
                final File to = new File(file.getAbsolutePath() + System.currentTimeMillis());
                file.renameTo(to);
                to.delete();
    //            file.delete();
            }
            file.createNewFile();
            return file;
        }
    
        /**  
         * 在SD卡上创建文件夹  
         * @param dirName  
         * @return  
         */
        public static File createSDDir(String dirName) {
            File dir = new File(SDPATH + dirName);
            if(!dir.exists())
            {
               dir.mkdirs();
            }
            return dir;
        }
    
        /**  
         * 推断SD卡上的文件夹是否存在  
         * @param fileName  
         * @return  
         */
        public boolean isFileExist(String fileName) {
            File file = new File(SDPATH + fileName);
            return file.exists();
        }
        
        /**  
         * 删除文件  
         * @param fileName  
         * @return  
         */
        public static void deleteFile(String fileName) {
            try {
                File file = new File(fileName);
                if(file.exists())
                {
                    file.delete();
                }
            } catch (Exception e) {
                if(e!=null)
                {
                    e.printStackTrace();
                }
            }
          
        }
    }
    
    

    4、网络管理类NetworkUtil.java

    public class NetworkUtil {
        public static int getNetworkType(Context mContext) {
            try {
                final ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
                final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                return networkInfo.getType();
            } catch (Exception e) {
                return ConnectivityManager.TYPE_WIFI;
            }
        }
        
        public static boolean isNetworkUseful(Context mContext)
        {
            final ConnectivityManager connectivityManager = (ConnectivityManager) mContext  
                    .getSystemService(Context.CONNECTIVITY_SERVICE);  
            final NetworkInfo mobNetInfoActivity = connectivityManager  
                    .getActiveNetworkInfo();  
            if (mobNetInfoActivity == null || !mobNetInfoActivity.isAvailable()) {  
                return false;  
            }else
            {
                return true;
            }
        }
    }

    5、測试TestDownloadActivity.java

    /**
     * 
     */
    package com.figo.study.activity;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    import com.figo.study.R;
    import com.android.inputmethod.latin.mgr.DownloadListener;
    import com.android.inputmethod.latin.view.HorizontalProgressBarWithNumber;
    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.FileAsyncHttpResponseHandler;
    
    /**
     * @author avazu
     *
     */
    public class TestDownloadActivity extends Activity {
        private HorizontalProgressBarWithNumber mProgressBar, mProgressBar1;
        private Button btnDownload, btnCancel, btn_pause_continue, btnDownload1, btnCancel1;
        AsyncHttpClient client = new AsyncHttpClient();
        FileAsyncHttpResponseHandler fileAsyncHttpResponseHandler = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_download_test);
            initView();
        }
    
        private void initView() {
            mProgressBar = (HorizontalProgressBarWithNumber) findViewById(R.id.id_progressbar);
            mProgressBar1 = (HorizontalProgressBarWithNumber) findViewById(R.id.id_progressbar1);
            //        btn_pause_continue = (Button) findViewById(R.id.btn_pause_continue);
            //        btn_pause_continue.setOnClickListener(new OnClickListener() {
            //
            //            @Override
            //            public void onClick(View v) {
            //                // TODO Auto-generated method stub
            //                fileAsyncHttpResponseHandler.sendRetryMessage(0);
            //            }
            //        });
            final String url = "http://dl.wandoujia.com/files/phoenix/latest/wandoujia-wandoujia_web.apk?timestamp=1409388568830";
            final String md5 = "";
            final String dirPath = "study/download/";
            final String fileName = "豌豆荚.apk";
            btnDownload = (Button) findViewById(R.id.btn_download);
            try {
                btnDownload.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
    
                     
                        BaseSkinApplication.shared().getDm().setDownloadWifiOnly(false);
                        BaseSkinApplication.shared().getDm().download(TestDownloadActivity.this, url, md5, dirPath, fileName, new DownloadListener() {
    
                            @Override
                            public void onStart() {
                                btnDownload.setVisibility(View.GONE);
                                btnCancel.setVisibility(View.VISIBLE);
    
                            }
    
                            @Override
                            public void onError(String errMessage) {
    
                            }
    
                            @Override
                            public void onSuccess() {
    
                            }
    
                            @Override
                            public void onProgress(int progress) {
                                mProgressBar.setProgress(progress);
                            }
                        });
    
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            final String url1 = "http://msoftdl.360.cn/mobilesafe/shouji360/360safesis/360MobileSafe.apk";
            final String md51 = "";
            final String dirPath1 = "study/download/";
            final String fileName1 = "360.apk";
            btnDownload1 = (Button) findViewById(R.id.btn_download1);
            try {
                btnDownload1.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
    
                  
    
                        BaseSkinApplication.shared().getDm().setDownloadWifiOnly(false);
                        BaseSkinApplication.shared().getDm().download(TestDownloadActivity.this, url1, md51, dirPath1, fileName1, new DownloadListener() {
    
                            @Override
                            public void onStart() {
                                btnDownload1.setVisibility(View.GONE);
                                btnCancel1.setVisibility(View.VISIBLE);
    
                            }
    
                            @Override
                            public void onError(String errMessage) {
    
                            }
    
                            @Override
                            public void onSuccess() {
    
                            }
    
                            @Override
                            public void onProgress(int progress) {
                                mProgressBar1.setProgress(progress);
                            }
                        });
    
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            btnCancel = (Button) findViewById(R.id.btn_cancel);
            btnCancel.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    BaseSkinApplication.shared().getDm().cancelDownload(TestDownloadActivity.this, url);
                    btnDownload.setVisibility(View.VISIBLE);
                    btnCancel.setVisibility(View.GONE);
                }
            });
            btnCancel1 = (Button) findViewById(R.id.btn_cancel1);
            btnCancel1.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    BaseSkinApplication.shared().getDm().cancelDownload(TestDownloadActivity.this, url1);
                    btnDownload1.setVisibility(View.VISIBLE);
                    btnCancel1.setVisibility(View.GONE);
                }
            });
    
        }
    }
    
    



  • 相关阅读:
    Web 前端开发中一些兼容性问题及其解决办法 (持续更)
    沈逸的IT专栏---shenyisyn
    程序员该做的事
    职场需要切忌的几点——《程序员,你伤不起》
    百度天气API接口
    C#简单音乐播放器ListBox歌单列表
    CentOS7 安装java(jre 1.8)
    CentOS7 系统安装与设置
    这个可以有,记录下
    在centos 7中让网卡自启动
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7326215.html
Copyright © 2020-2023  润新知