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.

4

6 回答 6

1

谢谢您的帮助。对不起,如果我不够清楚。

我找到了一种方法。基本上它相当简单,即在服务器端的 shell 脚本中附加一行额外的代码,以在生成图像的同时生成现有图像 URL 的文本列表。

之后,我在应用程序上使用了 BufferedReader 和 openStream 的组合将远程文本文件解析为字符串数组。

于 2012-04-18T03:50:56.527 回答
0

根据您的情况,您需要具备以下条件:

1- 一个 Web 服务,它有一种方法可以让您获得可用图像名称的列表。

2- 你的安卓应用程序需要一个 Web 服务客户端,我建议你使用KSOAP 2,因为它广为人知且易于实现。(如果您不知道如何在程序中使用 ksoap,我可以为您提供一些示例代码)

3-您需要使用 ArrayList(java.util) 来保存动态大小的数组。

于 2012-04-07T08:31:29.907 回答
0

Hey ytou 可以通过
ArrayList stringList = new ArrayList();

stringList.add("Item");
于 2012-04-07T08:32:27.113 回答
0

使用数组,您可以:

  • 改变数组的元素

但你不能:

  • 添加或删除元素。数组中固定的元素数。可以找到一些解决方法,例如在使用数组中的值时放置空值并丢弃它们。但这比真正有用更麻烦。

另一方面,如果你想要一个完整的动态“数组”:使用一个列表(java.util.List)。ArrayList 在这里会很有趣,甚至是 Vector,因为您可能需要围绕这个数组进行一些多线程处理。使用列表,您可以添加和删除元素,大小可以变化并且可以替换元素。

于 2012-04-07T08:29:04.147 回答
0

在这种情况下,我会使用 ArrayList。您不必知道要添加的元素数量,并且在最后附加元素非常简单。

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

然后只需添加元素

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

问候,帕特里克

于 2012-04-07T08:29:06.573 回答
0

而不是使用 Array of String 使用ArrayList<String>它将为您在运行时添加和删除项目提供更大的灵活性,请参阅此链接... http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ ArrayList.html

听说您可以在 arraylist 上找到示例...http://www.java2s.com/Tutorial/Java/0140_ Collections/0160 _ArrayList.htm

希望有帮助

于 2012-04-07T08:30:21.360 回答