【问题标题】:Downloading latest file version from server从服务器下载最新文件版本
【发布时间】:2014-03-22 03:45:32
【问题描述】:

您好,我有一个应用程序正在尝试添加内置更新程序。该应用程序包含一个从我的服务器下载 apk 文件的按钮。服务器上的文件名命名如下:

"App-1.apk", "App-2.apk", “App-3.apk” ...

按钮当前设置如下:

download = (Button) findViewById(R.id.bDownload);
versionNum = 3;

download.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent downloadFromServer = new Intent();
        downloadFromServer.setAction(Intent.ACTION_VIEW);
        downloadFromServer.addCategory(Intent.CATEGORY_BROWSABLE);
        downloadFromServer.setData(Uri.parse("http://server.com/Files/App-" + versionNum + ".apk"));
        startActivity(downloadFromServer);
    }
}); 

什么是检查服务器上可用的最高应用版本并选择下载的好方法?

编辑:如何使用 java 检查服务器目录中编号最高的应用程序?

Edit2:这就是我最终做的事情。不是最好的解决方案,但现在已经足够了:

try {
    PackageInfo appInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    installedVersion = appInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
    // Handle exception
}   

//Latest version available on my server. Must update this value for each new release
latestVersion = 3.1;

//Convert string value of installed version to double so that it can be compared with value of latest version       
installedVersionValue = Double.parseDouble(installedVersion); 

download = (Button) findViewById(R.id.bDownload);

download.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        if (installedVersionValue<latestVersion) { //If latest version available on server is greater than installed version, download the latest
            Intent downloadFromServer = new Intent();
            downloadFromServer.setAction(Intent.ACTION_VIEW);
            downloadFromServer.addCategory(Intent.CATEGORY_BROWSABLE);
            downloadFromServer.setData(Uri.parse("http://server.com/Files//App-" + latestVersion + ".apk"));
            startActivity(downloadFromServer);
        }
        else if (installedVersionValue==latestVersion) { //If user clicks the update button while they already have the latest, let them no what's up
            Toast.makeText(getApplicationContext(), "You are already running the latest version (" + installedVersionValue +")",
            Toast.LENGTH_LONG).show();  
        }
    }
}); 

【问题讨论】:

  • 您是在询问将在服务器上执行的代码吗?
  • 不,我正在寻找应用程序中的 java 解决方案。我不打算对服务器进行任何修改或发生任何事情。
  • 服务器能否发送它包含的应用程序可以扫描的文件列表?

标签: java android


【解决方案1】:

您可以在清单中添加代码版本或应用版本,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.package.name"
      android:versionCode="2"
      android:versionName="1.1">

你可以检查一下:

PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
int versionNumber = pinfo.versionCode;

您可以在服务器上的数据库表中将代码数设置为名称,然后检查该字段以更新您的应用程序。

你可以试试这样的:

向服务器传递这样的请求:youServer/apkUpdate?versionCode={versionCode}

在您的服务器中,您使用这样的方法:

@RequestMapping(method = RequestMethod.GET)
public void getUpdatedApk(
        @RequestParam(value = "versionCode", required = true) final Integer versionCode,
        @RequestParam(value = "androidVersion", required = false) final Integer androidVersion,
        final HttpServletRequest request, 
        final HttpServletResponse response) throws Exception{

    apkUpdateService.getUpdate(new Long(versionCode), response);

}

api更新服务在哪里:

public void getUpdate(Long currentVersionCode, HttpServletResponse response) throws Exception {
            //get latest number of apk from a db field
    Long dbVersionCode = apkUpdateRepository.findLastVersion();
    ServletOutputStream servletOutputStream = null;

    if (currentVersionCode == dbVersionCode){
        LOG.info("You have the last version");
        return;
    }

    if (currentVersionCode < dbVersionCode){
        FileInputStream inputStream = null;
        String filename = String.format(pathToApk, dbVersionCode);

        try{
        inputStream = new FileInputStream(filename);


        servletOutputStream = response.getOutputStream();

        byte[] buffer = new byte[1024];
        int bytesRead = 0;

        while ((bytesRead = inputStream.read(buffer)) != -1) {
            servletOutputStream.write(buffer, 0, bytesRead);
        }

        servletOutputStream.flush();
        LOG.info("File streamed");

        LOG.info("Download "+filename);
        }finally{
            if (inputStream!=null){
                inputStream.close();
            }
            if (servletOutputStream != null){
                servletOutputStream.close();
            }
        }
    }
}

【讨论】:

  • 但是我如何检查服务器上文件的版本号是多少?
  • @Teo 我已经看过这段代码了.. 也许 ascensori?哈哈哈
【解决方案2】:

我认为最简单的方法是按升序检查文件是否存在,而不是下载最后一个存在的文件。文件存在检查已在此处得到解答:Check if file exists on remote server using its URL

【讨论】:

    【解决方案3】:

    你可以例如:

    String installedVersion = "";
            try {
                PackageInfo manager = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0);
                installedVersion = manager.versionCode + "";
            } catch (PackageManager.NameNotFoundException e) {
                // Handle exception
            }    
    "http://server.com/Files/update?installed=" + installedVersion
    

    在服务器端,您可以执行以下操作:

    int installedVersion = ...
    int latestVersion = ...
    if(installedVersion < latestVersion){
    //return file
    }else{
     //Return message: you have the latest
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-27
      • 2012-11-13
      • 1970-01-01
      • 2020-02-29
      • 2018-06-18
      • 2014-07-27
      • 1970-01-01
      • 2021-09-28
      相关资源
      最近更新 更多