美文网首页
OkHttp下载

OkHttp下载

作者: 小慧sir | 来源:发表于2019-07-16 10:34 被阅读0次
    (https://alissl.ucdl.pp.uc.cn/fs08/2019/07/05/1/110_17e4089aa3a4b819b08069681a9de74b.apk)
    
    public class AppOkHttpDownload01 extends AppCompatActivity implements View.OnClickListener {
    
        private ProgressBar mProgDownload;
        /**
         * 下载
         */
        private Button mButDownload;
        private Context context;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_app_ok_http_download01);
            context = getApplicationContext();
            initView();
        }
    
        private void initView() {
            mProgDownload = (ProgressBar) findViewById(R.id.prog_download);
            mButDownload = (Button) findViewById(R.id.but_download);
            mButDownload.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                default:
                    break;
                case R.id.but_download:
                    Executors.newCachedThreadPool().execute(new Runnable() {
                        @Override
                        public void run() {
    
                            try {
                                OkHttpClient client = new OkHttpClient.Builder().build();
                                Request request = new Request.Builder()
                                        .url("https://alissl.ucdl.pp.uc.cn/fs08/2019/07/05/1/110_17e4089aa3a4b819b08069681a9de74b.apk")
                                        .get()
                                        .build();
                                Call call = client.newCall(request);
                                Response response = call.execute();
                                //获取下载的内容输入流
                                ResponseBody body = response.body();
                                InputStream inputStream = body.byteStream();
                                final long lengh = body.contentLength();
                                System.out.println("文件大小" + lengh);
                                // 文件保存到本地
                                File file1 = Environment.getExternalStorageDirectory();
                                File file = new File(file1, "xxxx123.apk");
                                FileOutputStream outputStream = new FileOutputStream(file);
                                int lien = 0;
                                int losing = 0;
                                byte[] bytes = new byte[1024];
                                while ((lien = inputStream.read(bytes)) != -1) {
                                    outputStream.write(bytes, 0, lien);
    
                                    losing += lien;
                                    final float i = losing * 1.0f / lengh;
                                    System.out.println("下载进度" + i);
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            int ii = (int) (i * 100);
                                            mProgDownload.setProgress(ii);
                                        }
                                    });
                                    //使用rxjava切换线程
    //                               // UpdateAppearanceProgress(i);
                                }
                                outputStream.flush();
                                inputStream.close();
                                outputStream.close();
    
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    break;
            }
        }
    
        private void UpdateAppearanceProgress(final float i) {
            Observable.just(i)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Float>() {
                        @Override
                        public void accept(Float aFloat) throws Exception {
                            int ii = (int) (aFloat * 100);
                            mProgDownload.setProgress(ii);
                        }
                    });
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:OkHttp下载

          本文链接:https://www.haomeiwen.com/subject/fdrzkctx.html