android 利用okhttp实现apk下载,带进度

Posted Dr_abandon新秀

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 利用okhttp实现apk下载,带进度相关的知识,希望对你有一定的参考价值。

下载类

 /**
     * 文件下载
     *
     * @param url 下载的连接
     */
    public void downFile(String url) 
        ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setTitle(getResources().getString(R.string.s_ing_download));
        progressDialog.setMessage(getResources().getString(R.string.s_please_wait));
        progressDialog.setProgress(0);
        progressDialog.setMax(100);
        progressDialog.show();
        progressDialog.setCancelable(false);
        DownloadUtil.get().download(url, Environment.getExternalStorageDirectory().getAbsolutePath() + "/apk/", "apk.apk", new DownloadUtil.OnDownloadListener() 
            @Override
            public void onDownloadSuccess(File file) 
                if (progressDialog != null && progressDialog.isShowing()) 
                    progressDialog.dismiss();
                
                //下载完成进行相关逻辑操作
                Log.e("====", "=======>" + file.getPath());
              
            

            @Override
            public void onDownloading(int progress) 
                progressDialog.setProgress(progress);
            

            @Override
            public void onDownloadFailed(Exception e) 
                //下载异常进行相关提示操作
              
            
        );
    

下载的工具类

/**
 * 文件下载工具类(单例模式)
 *
 */

public class DownloadUtil 
    private static DownloadUtil downloadUtil;
    private final OkHttpClient okHttpClient;

    public static DownloadUtil get() 
        if (downloadUtil == null) 
            downloadUtil = new DownloadUtil();
        
        return downloadUtil;
    

    private DownloadUtil() 
        okHttpClient = new OkHttpClient();
    

    /**
     * @param url          下载连接
     * @param destFileDir  下载的文件储存目录
     * @param destFileName 下载文件名称
     * @param listener     下载监听
     */
    public void download(final String url, final String destFileDir, final String destFileName, final OnDownloadListener listener) 


        File f1 = new File(destFileDir,destFileName);
        if (f1.exists())
            f1.delete();
        


        Request request = new Request.Builder().url(url).build();
        okHttpClient.newCall(request).enqueue(new Callback() 
            @Override
            public void onFailure(Call call, IOException e) 
                // 下载失败监听回调
                listener.onDownloadFailed(e);
            

            @Override
            public void onResponse(Call call, Response response) throws IOException 
                InputStream is = null;
                byte[] buf = new byte[2048];
                int len = 0;
                FileOutputStream fos = null;
                // 储存下载文件的目录
                File dir = new File(destFileDir);
                if (!dir.exists()) 
                    dir.mkdirs();
                
                File file = new File(dir, destFileName);
                try 
                    is = response.body().byteStream();
                    long total = response.body().contentLength();
                    fos = new FileOutputStream(file);
                    long sum = 0;
                    while ((len = is.read(buf)) != -1) 
                        fos.write(buf, 0, len);
                        sum += len;
                        int progress = (int) (sum * 1.0f / total * 100);
                        // 下载中更新进度条
                        listener.onDownloading(progress);
                    
                    fos.flush();
                    // 下载完成
                    listener.onDownloadSuccess(file);
                 catch (Exception e) 
                    listener.onDownloadFailed(e);
                 finally 
                    try 
                        if (is != null)
                            is.close();
                     catch (IOException e) 
                    
                    try 
                        if (fos != null)
                            fos.close();
                     catch (IOException e) 
                    
                
            
        );
    

    public interface OnDownloadListener 
        /**
         * @param file 下载成功后的文件
         */
        void onDownloadSuccess(File file);

        /**
         * @param progress 下载进度
         */
        void onDownloading(int progress);

        /**
         * @param e 下载异常信息
         */
        void onDownloadFailed(Exception e);
    

以上是关于android 利用okhttp实现apk下载,带进度的主要内容,如果未能解决你的问题,请参考以下文章

android 利用okhttp实现apk下载,带进度

Android使用OkHttp实现带进度的上传下载

Android中实现OkHttp上传文件到服务器并带进度

Android编程入门--开源框架OKHttp

带你走通 OkHttp+Retrofit+Rxjava

带你走通 OkHttp+Retrofit+Rxjava