下载服务器中的apk

/**
* 下载服务器中的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

        }
    });

}

猜你喜欢

转载自blog.csdn.net/luochuanrong/article/details/51863783