有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将数据从服务器中的文本文件提取到Android应用程序

我们有一个应用程序,我们正在使用它从服务器获取一些图像,我们通过直接URL获取来实现。此外,还有一些其他数据以及每个URL,如其标题、缩略图URL、原始链接等

下面是我们当前获取数据的方式

   public class Images {

        public final static String[] Image_webLinks = new String[] {
                "http://www.example.com/ImageURL_1/",
                "http://www.example.com/ImageURL_2/",
                "http://www.example.com/ImageURL_3/",
        };


        public final static String[] Image_Titles = new String[] {
                "My Image 1",
                "My Image 2",
                "My Image 3",
        };

        public final static String[] ImageView_Urls = new String[] {
                "http://www.example.com/Image_1.jpg",
                "http://www.example.com/Image_2.jpg",
                "http://www.example.com/Image_3.jpg",
        };

        public final static String[] ThumbnailUrls = new String[] {
                "http://www.example.com/thumbnail_1.jpg",
                "http://www.example.com/thumbnail_2.jpg",
                "http://www.example.com/thumbnail_3.jpg",
        };
    }

我们知道这可能不是从服务器获取数据的正确方法,但我们有一个简单的要求。有没有一种方法可以将字符串数据存储在某个文本文件中,并直接从网上获取,从而每次都更新字符串数据,如果我们想在将来添加额外的URL,那么我们不需要更新代码。我们只需要更新服务器内部的文本文件。可能吗?怎么做


共 (2) 个答案

  1. # 1 楼答案

    here所述使用截击。这是推荐的方法

  2. # 2 楼答案

    你可以使用这个代码

     new AsyncTask<String, Void, String>()
            {
    
                @Override
                protected String doInBackground(String[] params) {
                    try
                    {
                        String line;
                        URL url = new URL("http://nomediakings.org/everyoneinsilico.txt");
                        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                        String str="";
                        while ((line = in.readLine()) != null) {
                            str+=line;
                        }
                        in.close();
                    }catch (Exception ex)
                    {
                        ex.getMessage();
                    }
                    return null;
                }
            }.execute("");