手动.apk更新随机失败(从服务器下载)

时间:2013-02-01 08:54:41

标签: android android-asynctask apk auto-update

应用程序更新在启动时在应用程序中发生。应用程序在服务器上检查最新版本,如果有更新版本,则下载到存储最新的.apk文件。然后,应用程序启动更新过程。

有时会发生这种情况,即下载并尝试安装更新后会出现一条消息。 “解析错误。解析包时出现问题。”我们在这个领域有60多个Android设备,除了大约10个以外,大多数设备都有更新。

然后,在使用相同更新过程的新项目中,我们有平板电脑拒绝使用相同的错误消息进行更新。这种情况在同一台平板电脑上反复出现,虽然在另一台平板电脑(不同的品牌和操作系统)上运行良好。

设备品牌或操作系统版本之间似乎没有相关性。在平板电脑上,我们发现.apk文件显然不能完全下载。但是,从互联网浏览器进行手动下载和更新可以正常工作。

因此,尝试在某些设备上总结.apk文件下载未完成,安装失败。这可能是因为网络带宽限制吗?但是,由于更新过程是在每次应用程序启动时启动的,因此该领域的10个平板电脑肯定会更新,我可以看到这些应用程序一次又一次地使用但未更新。

很抱歉在漫无目的,但我想了解很多信息。以下这些代码行可以在5秒后停止下载吗?两次超时有什么区别?

HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);

以下是标准更新AsyncTask。任何帮助,将不胜感激。我现在有点困惑,时间不在我身边。

   private class UpdateDownloadTask extends AsyncTask<Void, Void, String> {

        HttpClient httpclient;
        HttpGet httpget;
        HttpResponse response;
        HttpEntity httpentity;
        OutputStream outputStream;

        protected void onPreExecute () {

            //do not lock screen or drop connection to server on login
            activity.getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);                  
            //initiate progress dialogue to block user input during initial data retrieval
            ProcessingDialog = ProgressDialog.show(context, "Please Wait", "Downloading Updates", true,false);
        }

        @Override
        protected String doInBackground(Void... nothing) {

            try {
                //set timeouts for httpclient
                HttpParams httpParameters = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
                HttpConnectionParams.setSoTimeout(httpParameters, 5000);

                //setup http get
                httpclient = new DefaultHttpClient(httpParameters);             
                httpget = new HttpGet(uriApk);

                // Execute HTTP Get Request
                response = httpclient.execute(httpget);
                httpentity = response.getEntity();

                //create location to store apk file
                String path = Environment.getExternalStorageDirectory() + "/download/";
                File file = new File(path);
                file.mkdirs();  //if download folder not already exist
                File outputFile = new File(file, apkName);

                //write downloaded file to location
                outputStream = new FileOutputStream(outputFile, false);
                httpentity.writeTo(outputStream);
                outputStream.flush();
                outputStream.close();

                return "success";
            }

            catch (Exception e) {
                return "error: " + e.toString();
            }

        }

        @Override
        protected void onPostExecute(String result) {

            //check if result null or empty
            if (result.length() == 0 || result == null) {

                Toast.makeText(context, "Could Not Download Updates, Please Try Again. Closing Application.", Toast.LENGTH_LONG).show();
                activity.finish();
            }           
            //update downloaded
            if (result.equals("success"))   {
                //install downloaded .apk file
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + apkName)), "application/vnd.android.package-archive");
                activity.startActivity(intent);               
                //activity.finish();
            }
            //update not downloaded
            else {
                Toast.makeText(context, "Could Not Download Updates, Please Try Again. Closing Application.", Toast.LENGTH_LONG).show();                
                activity.finish();
            }           

            //close update dialog
            try {
                ProcessingDialog.dismiss();
            } catch (Exception e) {
                // nothing
            }
            //release screen lock
            activity.getWindow().clearFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }

1 个答案:

答案 0 :(得分:0)

在我添加以下代码并将超时时间延长到30000毫秒后,它才有效。

httpget.setHeader("Accept", "application/vnd.android.package-archive");
httpget.setHeader("Content-Type", "application/vnd.android.package-archive");
String userAgent = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; SCH-I800 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
httpget.setHeader("User-Agent", userAgent); 

希望这可以帮助其他人解决同样的问题。