【问题标题】:In-application update downloads from server not working. Service provider error?从服务器下载的应用程序内更新无法正常工作。服务提供商错误?
【发布时间】:2013-02-04 09:56:36
【问题描述】:

我正在开发一个独立于 Android Play 商店进行更新的应用。在启动时,它会检查更新的版本并在需要时从服务器下载新的 .apk 文件。

问题是通过 HTTP 请求执行的 .apk 文件下载 url 被重定向到要求更改移动互联网设置的 Vodafone 登录页面。那么应用下载的.apk文件其实就是这个html网页。

通过安卓互联网浏览器访问任何网站都不会被重定向到这个页面。我在托管服务器上的 MIME 设置已正确设置。我在另一个项目上使用相同的代码、托管服务提供商和移动运营商没有任何问题,尽管该项目在 OS 3.2 上运行东芝平板电脑,而这个项目是在 OS 4.1 上运行的三星平板电脑。

我可以测试或更改什么以使其正常工作,还是与运营商或平板电脑/操作系统相关的问题?任何帮助,将不胜感激。如果有人想检查它,相关代码包含在下面,但如前所述,我在另一个项目上没有问题。

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);
}
}

【问题讨论】:

    标签: android download apk auto-update


    【解决方案1】:

    我必须通过 Android 互联网浏览器将运营商设置设置为“不调整任何网站”。之后,我添加了以下代码以使其正常工作。

    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); 
    

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

    【讨论】:

      猜你喜欢
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 2014-10-20
      相关资源
      最近更新 更多