2 leaf 130 leaf_130 于 2016.03.16 18:38 提问

下载服务器的apk文件程序出现异常 5C

图片说明
图片说明
图片说明
代码如下:
public class AsyncTaskTestActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_async);
}

private File apkFile;
private ProgressDialog dialog;

public void downloadApk(View v) {
    //启动异步任务处理
    new AsyncTask<Void, Integer, Void>() {

        //1. 主线程, 显示提示视图
        protected void onPreExecute() {
            dialog = new ProgressDialog(AsyncTaskTestActivity.this);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.show();

            //准备用于保存APK文件的File对象 : /storage/sdcard/Android/package_name/files/xxx.apk
            apkFile = new File(getExternalFilesDir(null), "update.apk");

        }

        //2. 分线程, 联网请求
        @Override
        protected Void doInBackground(Void... params) {
            try {
                //1. 得到连接对象
                String path = "http://172.16.12.8:8080/Web_Server/App10_animation.apk";
                URL url = new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                //2. 设置
                //connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(10000);
                //3. 连接
                connection.connect();
                //4. 请求并得到响应码200
                int responseCode = connection.getResponseCode();
                if(responseCode==200) {
                    //设置dialog的最大进度
                    dialog.setMax(connection.getContentLength());


                    //5. 得到包含APK文件数据的InputStream
                    InputStream is = connection.getInputStream();
                    //6. 创建指向apkFile的FileOutputStream
                    FileOutputStream fos = new FileOutputStream(apkFile);
                    //7. 边读边写
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    while((len=is.read(buffer))!=-1) {
                        fos.write(buffer, 0, len);
                        //8. 显示下载进度
                        //dialog.incrementProgressBy(len);
                        //在分线程中, 发布当前进度
                        publishProgress(len);

                        //休息一会(模拟网速慢)
                        //Thread.sleep(50);
                        SystemClock.sleep(50);
                    }

                    fos.close();
                    is.close();
                }
                //9. 下载完成, 关闭, 进入3)
                connection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        //3. 主线程, 更新界面
        protected void onPostExecute(Void result) {
            dialog.dismiss();
            installAPK();
        }

        //在主线程中更新进度(在publishProgress()之后)
        protected void onProgressUpdate(Integer[] values) {
            dialog.incrementProgressBy(values[0]);
        }
    }.execute();

    //int Integer float Float void Void
}

/**
 * 启动安装APK
 */
private void installAPK() {
    Intent intent = new Intent("android.intent.action.INSTALL_PACKAGE");
    intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
    startActivity(intent);
}

}

1个回答

lengcj
lengcj   2016.03.16 20:02

网络不好吧~重下啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!