okhttp下载

来源:互联网 发布:c语言一维数组最大长度 编辑:程序博客网 时间:2024/06/07 15:51
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);                        }                    }                });    }}