1

Hi I'm not particular good at Java so please bear with me. I'm trying to write a very simple android app now and I need help with some coding.

Thing is, I have a server that automatically generates .png files and saves them to a public directory in a numerical order. The update occurs daily and is non-exhaustive.

Is there anyway in which I can assign the dynamic values to an array within my app?

            private String[] myRemoteImages = {
            "http://hypotheticalurl1.png",
            "http://hypotheticalurl2.png",
            "http://hypotheticalurl3.png",
            "http://hypotheticalurl4.png",
            "http://hypotheticalurl5.png",
            "http://hypotheticalurl6.png",
            "http://hypotheticalurl7.png",
            "http://hypotheticalurl8.png",
            "http://hypotheticalurl9.png",
            "http://hypotheticalurl10.png",
            "http://hypotheticalurl11.png",
            "http://hypotheticalurl12.png",
            //...blah blah blah
            // these are all dynamically created so I won't know what is the last number on the list
    };

This array will eventually be used to get the images from my server using the app. It works so far but that's only with hardcoded URLs. I would like the URLs to be dynamic, as the number of images will change from day to day.

I'm doubting that regex will work well in Java but then again I'm no expert. Was thinking of perhaps writing a script on the server end that generates a list of existing values and somehow parsing that with the android app.

Can anyone point me in the right direction? Thanks in advance.

Clarification:

The array doesn't have to be dynamically sized while the app is running.

I need a way to read the list of existing images in a remote directory and pass that information to populate the array automatically at runtime.

Resolved

Guys, thanks for the help. Sorry if I wasn't clear enough.

I've found a way to do it. Basically it was rather simple, which was to append an extra line of code to the shell script on the server end to generate a text list of existent image URLs at the same time that it generates the images.

After that, I used a combination of BufferedReader and openStream on the app to parse the remote text file into a String array.

Reuben L.
  • 2,806
  • 2
  • 29
  • 45
  • but u can convert this Array to `ArrayList` like `ArrayList assetList = Arrays.asList(myRemoteImages);` then use `assetList.size()` to get size of array list u have any prob with this way? – ρяσѕρєя K Apr 07 '12 at 08:22
  • myRemoteImages array coming from server in which format json or xml api? – ρяσѕρєя K Apr 07 '12 at 08:24
  • the array is declared in the app itself. the server merely generates the images at this point in time. there is no passing of information between the two as the URLs are currently hardcoded. – Reuben L. Apr 07 '12 at 08:28
  • ok then can u edit question with variable in which u are receiving response from server – ρяσѕρєя K Apr 07 '12 at 09:05

6 Answers6

1

thanks for the help. Sorry if I wasn't clear enough.

I've found a way to do it. Basically it was rather simple, which was to append an extra line of code to the shell script on the server end to generate a text list of existent image URLs at the same time that it generates the images.

After that, I used a combination of BufferedReader and openStream on the app to parse the remote text file into a String array.

Reuben L.
  • 2,806
  • 2
  • 29
  • 45
0

With an array you can :

  • change the elements of the array

but you can't :

  • add or remove elements. The number of elements if fixed in an array. Some workaround can be found like putting null values and discarding theem when using the values in the array. But that's more troublesome than really useful.

On the other hand, if you want a full dynamic "array" : use a list (java.util.List). An ArrayList would be interesting here, or even a Vector as you will probably need some multihtreading around this array. With a list you can add and remove elements, size can vary and elements can be replaced.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • And if you have some free time and are free to explore some nice technology, try guava. http://code.google.com/p/guava-libraries/ – Snicolas Apr 07 '12 at 08:30
  • oh wait i think i wasn't clear enough. or maybe used the terms wrongly. the app doesnt have to dynamically recreate the array while it is running. it just needs to have the right array each runtime. – Reuben L. Apr 07 '12 at 08:41
  • Ain't you getting your download urls during program life cycle ? You should. – Snicolas Apr 07 '12 at 10:21
0

I'd use an ArrayList in this case. You don't have to know the number of elements you want to add then and it's very simple to append elements at the end.

private List<String> list = new ArrayList<String>();

Then simply add elements by

list.add("http://hypotheticalurl1.png");

Regards, Patrick

Patrick
  • 137
  • 1
  • 8
  • sounds like a choice method for most of the answers here. thing is, i need a way to KNOW the list of URLs as well since I won't know when the numbering ends as it is being updated on its own everyday. – Reuben L. Apr 07 '12 at 08:43
0

instead of using Array of String use ArrayList<String> It will gives you more flexibility on adding and removing item on runtime refer this link...http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

hear you can find example on arraylist...http://www.java2s.com/Tutorial/Java/0140_Collections/0160_ArrayList.htm

hope that helps

Nixit Patel
  • 4,435
  • 5
  • 30
  • 57
0

According to your scenario you need to have the followings:

1- a Web Service which has a method to get you the list of the available image names.

2- You need a web service client for your android application, I suggest you to use KSOAP 2 because it is widely known and easy to implement. (If you can't figure out how to use the ksoap in your program, I can provide you some example codes)

3- You need to use ArrayList(java.util) to hold your dynamically sized array.

Gunhan
  • 6,807
  • 3
  • 43
  • 37
0

Hey ytou can do it via
ArrayList stringList = new ArrayList();

stringList.add("Item");
Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69