使用相同的urlconnection从一台服务器加载多个文件

时间:2014-03-18 15:20:32

标签: android download urlconnection

我是 Android / Java 开发的新手。目前我尝试开发一个显示几个食堂菜单的应用程序。 存储相关信息的URL如下所示(仅示例):

http://canteen-example.com/canteen-munich/14.csv  ("14" represents week of year)
http://canteen-example.com/canteen-berlin/14.csv
http://canteen-example.com/canteen-cologne/14.csv
...

目前,我为每个网址使用自己的HttpUrlConnection下载这些文件。它的工作正常,但是当手机的连接很弱时,它有点慢。我认识到,在这种情况下,连接到服务器和下载文件的时间过长非常快(只有纯文本)。

我为每个URL调用的方法(在后台)中看起来像这样:

private String downloadUrl(String myurl) throws IOException {
    InputStream is = null;

    try {
        //I want to connect only once to the server which they have all in common and not for each URL
        URL url = new URL(myurl);
        conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect();

        statusCode = conn.getResponseCode();
        is = conn.getInputStream();

        return readIt(is);
    }
    finally {
        if (is != null) {
            is.close();
        }
    }
}

但是现在问题是,是否有可能只连接一次这个特定的服务器(所有URL都相同),然后从不同的路径下载这些“.csv”文件?

由于

0 个答案:

没有答案