okhttp下载

1、依赖
compile 'com.zhy:okhttputils:2.6.2'

2、代码
public class MainActivity extends AppCompatActivity {

    private File file;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar) findViewById(R.id.progress);
        OkHttpUtils.get()
                .url("http://huixinguiyu.cn/Assets/js/newsnew.js")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        String string = response.toString();
                        Toast.makeText(MainActivity.this,string, Toast.LENGTH_SHORT).show();
                    }
                });
        file = new File(Environment.getExternalStorageDirectory().getPath());

    }
    public void bbb(View v){
        progressBar.setVisibility(View.VISIBLE);
        OkHttpUtils
                .get()
                .url("http://p.gdown.baidu.com/e96035c05cfbd104bc614338d32e4998cf247dcb6b1d2f55352c28c485a21dc66ba8b52319123d749562b790fc47aaaee5da5908708263ce0b1692be82a460934eed49f570b9c59411293bb6bbc5eaa6b5d33a12fa8942809ab5006d401340245313c56321cf811b955d8594032edf172207325d3d4fe11fd2a043066ab78e7def5069cf551eefddc1250aa4f4faf971cddb65081a3004655f51e0adfa56339673d241fd87d214181c62654fcf4ec14746c495a66bbafe057a887ec1a8741de6fec48d8ed0e75ab7e22a9538a70aeb0d590439066f50f5505137d99b62a87fa5")
//                .url("https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=2207238342,3445927478&fm=58&u_exp_0=1640204727,3112622068&fm_exp_0=86&bpow=512&bpoh=512")
                .build()
                .execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(),"shf.apk") {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(MainActivity.this,e+"",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void inProgress(float progress, long total, int id) {
                        progressBar.setProgress((int)(100*progress));

                    }

                    @Override
                    public void onResponse(File response, int id) {
                        String string = response.toString();
                        Toast.makeText(MainActivity.this,string,Toast.LENGTH_LONG).show();

                        if (progressBar.getProgress()==progressBar.getMax()){
                            Toast.makeText(MainActivity.this,"ok",Toast.LENGTH_LONG).show();

                            Intent intent = new Intent(Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(response), "application/vnd.android.package-archive");
                            startActivity(intent);

                        }
                    }

                });
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值