0

我正在为 Android 和 BlackBerry 开发一个移动应用程序。

通过将版本代码和版本名称包含在服务器上的纯文本文件中,我已经上传了最新的 OTA 安装应用程序。

在我的应用程序中,如何将代码版本和版本名称从服务器上的纯文本格式转换为字符串。我想这样做,以便我可以让应用程序将其当前版本与服务器上可用的版本进行比较,这样如果服务器上有更新的版本,将向用户提供要下载的更新。

4

3 回答 3

2

假设您将文件上传到服务器,其中包含一行:

1.2.3.4

The process is similar for Android and BB:

1- Retrieve the file from the server. You'll probably have a byte array as result.

2- Convert it to a String with the proper encoding. In case the txt only contains numbers and dots, the encoding is not really important since these are ASCII chars, and ASCII chars are compatible with most usual default encodings like UTF-8 and ISO-8859. So we could probably instantiate the string without dealing with the encoding, like this: String fileContent = new String(byte[] downloadedData). Otherwise, make sure you know in advance the txt file encoding and instantiate the string with that encoding.

3- Split the string using the dots as separators. In Android you can do it like this: String[] splitted = String.split(fileContent, '.'), or use a StringTokenizer. In BB, as it is based in CLDC, this method in String is not available so you should code it yourself, or use/port one from a well tested library (like Apache Commons' org.apache.commons.lang3.StringUtils.split). After this step you'll have an array of strings, each string being a number ({"1","2","3","4"} in the example).

4- Now create a int array of the same length, and convert each string in the array to its equivalent int, using Integer.parseInt(splitted[i]) on each element i.

5- Get the version for your app and perform the same steps to get an array of int. In BB, you can call ApplicationDescriptor.currentApplicationDescriptor().getVersion(). In Android, PackageInfo.versionCode or PackageInfo.versionName, depending on what you have specified in the manifest.

6- Notice both arrays don't need to be of the same length. You could have written "1.2.3.4" in your txt, but have "1.2.3" in your AndroidManifest.xml or BlackBerry_App_Descriptor.xml. Normalize both resulting int arrays to have the same length (the lenght of the longer one), and fill the added elements with zeroes. Now you'll have two int arrays (in the example, txtVersion = {1,2,3,4} and appVersion = {1,2,3,0}). Iterate comparing versions one by one. The rule is: if txtVersion[i] > appVersion[i], then you are out of date and an upgrade is needed.

于 2012-09-12T10:30:28.483 回答
1

此答案仅适用于 android 部分。

要从您的应用程序中获取应用程序版本号和名称,您可以执行以下操作(如@ColorWP.com建议的那样

获取应用程序版本和名称:

String version_number = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
String version_name = getPackageManager().getPackageInfo(getPackageName(), 0).versionNumber;

从网上读取您的文件:

URL url = new URL("http://www.your.site/your.txt");
URLConnection connect = url.openConnection();

BufferedReader txtreader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
String line = txtreader.readLine();
while (line != null) {
    /* Code to Read your variables in this loop 
      (let us assume these would be:
      server_app_version
      server_app_name)
    */
 }

确保按照@ColorWP.com的建议添加 Internet 权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

比较版本:

if (server_app_version.compareTo(version_number) != 0 || server_app_name.compareTo(version_name) != 0){
    // Notify user to download the new version
}
于 2012-09-12T09:00:05.370 回答
0

与其使用 txt 文件,不如从您的服务器输出 JSON(Android 提供了一些有用的 JSON 解码和编码方法)。

在更新活动中设置将 JSON 输出为静态最终变量的页面的 URL,使用您喜欢的任何方法下载它。以下答案可能有用。

然后,使用此答案中的代码解析 JSON 字符串。

当您从服务器以字符串或整数形式获取应用程序的最新版本时,您可以将其与您可以使用的本地应用程序版本进行比较:

PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pInfo.versionCode;
// To get the version name use pInfo.versionName

请记住添加 INTERNET 权限,以便您的应用可以在线连接:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
于 2012-09-12T09:08:33.927 回答