0

I am developing a mobile app, for both Android and BlackBerry.

I've uploaded my latest application for OTA installation by including version code and version name into a plain text file on the server.

Within my app, how do I convert code version and version name from the plain-text format on my server to a string. I want to do this so I can have the app compare it's current version against what is available on the server, so that if a newer version on the server, the user will be offered an update to download.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
oyiym
  • 11
  • 7

3 Answers3

2

Let's say you upload a file to your server, that contains a single line:

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.

Mister Smith
  • 27,417
  • 21
  • 110
  • 193
1

This answer is for the android part only.

To get the app version number and name from your application, you can do the following (as suggested by @ColorWP.com

Getting the app version and name:

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

To read your file from the net:

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)
    */
 }

Make sure you add Internet permission, as suggested by @ColorWP.com

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

Compare the version:

if (server_app_version.compareTo(version_number) != 0 || server_app_name.compareTo(version_name) != 0){
    // Notify user to download the new version
}
Community
  • 1
  • 1
Faisal___
  • 68
  • 1
  • 7
  • i forgotted that my app wouldn't upload to google play or app world but i'll upload it to my website and for the example, this is my url which include description of version code "http://mysite.com/mob/au.txt" how i access that URI so that i can read the content of .txt from android/blackberry – oyiym Sep 12 '12 at 09:03
  • Are you talking about a code within your application itself? I will edit my answer. For Google Play, you will need an encryption key to post your app. – Faisal___ Sep 12 '12 at 09:16
  • By the way, your text file seems to be inaccessible. – Faisal___ Sep 12 '12 at 10:01
  • i mean, i need manual code, not google play because i have known if i upload to google play, google will notif app automaticly thanks for edit, i'll try firstly :) – oyiym Sep 12 '12 at 10:10
  • i've tried your code but there are many bugs, help me please :'( – oyiym Sep 12 '12 at 10:23
  • What kind of bugs? Any feedback from Logcat? – Faisal___ Sep 12 '12 at 10:28
  • Make sure you import the appropriate packages: import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URI; import java.net.URL; import java.net.URLConnection; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager; – Faisal___ Sep 12 '12 at 10:34
  • i have imported all, nothing happened is there the best solution for me? as long as possible, thanks for helping me :) you're so kind – oyiym Sep 12 '12 at 11:01
  • You are welcome. Maybe if you can find out when the program crashes (i.e. which of the above section does not work), we can work from there. – Faisal___ Sep 13 '12 at 00:24
  • android fixed :), but i need your help for blackberry could you please :) – oyiym Sep 13 '12 at 04:03
  • Hi there! Unfortunately I cannot really help you with Blackberry. However, Mister Smith has mentioned the following in his answer: ApplicationDescriptor.current and ApplicationDescriptor().getVersion(). Maybe you can use my code and replace the android function to get version name and number by these (you will have to find out if you can get URI/URL in the same way though). – Faisal___ Sep 17 '12 at 08:17
0

Instead of using txt files it is better to output JSON from your server (Android provides some useful JSON decoding and encoding methods).

Setup the URL to the page that outputs the JSON as a static final variable in your Update activity, download it using any method you like. The following answer may be useful.

Afterwards, parse the JSON string by using the code in this answer.

When you get the newest version of the app from your server as string or int you can compare it to the local app's version which you can get using:

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

Remember to add the INTERNET permission so your app can connect online:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Community
  • 1
  • 1
Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118
  • your suggestion maybe more usefull, but I have not really understood using of jason, can you explain or give me more tutorial thanks before :) – oyiym Sep 12 '12 at 09:15
  • 2
    I agree in that JSON might be more standard, but really there's nothing bad on using a txt file. In terms of retrieved info length, it can be as short as the JSON or more, and some servers like Nginx can provide better performance for files than for a REST/SOAP call. – Mister Smith Sep 12 '12 at 09:39
  • so, Which do you think is better for me to use, and please give the tutorial, please – oyiym Sep 12 '12 at 10:13
  • @user1665082: I think the following tutorial (http://www.androidhive.info/2012/01/android-json-parsing-tutorial/) will be of help. It describes how to retrieve a JSON formatter remote file (from your server) and how to parse it in your Android application. Also, if you find this answer useful, don't forget to accept it. – Dzhuneyt Sep 12 '12 at 11:19
  • android fixed :), but i need your help for blackberry could you please :) – oyiym Sep 13 '12 at 04:03