安卓中下载APK到本地中(罗传榕)

来源:互联网 发布:php开发管理系统 编辑:程序博客网 时间:2024/05/17 03:30

/** 
* 下载服务器中的apk 
*/ 

private void getserviceVersion() { OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url(path).build(); okHttpClient.newCall(request).enqueue(new Callback() {        @Override        public void onResponse(Response arg0) throws IOException {            if (arg0.isSuccessful()) {                 File file = new File(getCacheDir(), "qq.apk");                InputStream is = arg0.body().byteStream();                FileOutputStream fos = new FileOutputStream(file);                byte[] buffer = new byte[1024];                while(!flag){                    int count = is.read(buffer);                    if(count<=0){                        break;                    }                    fos.write(buffer, 0, count);                }                fos.close();                is.close();            }        }        @Override        public void onFailure(Request arg0, IOException arg1) {            // TODO Auto-generated method stub        }    });}


0 0