unitedartinc
unitedartinc

Reputation: 748

Fetch Data from text file in a server to an Android app

We have an app that we are using it to fetch some Images from a server, and we are doing it through direct URL fetching. Also there are some other data along with each URL like its Title, Thumbnail URL, OriginalLinks etc.

Below is how we are fetching the data currently

   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",
        };
    }

We know that this may not be the right way to fetch data from the server, But we have a simple requirement.Is there a way to store the String data in some text file and fetch it directly from online and thus updating the string data every time, Doing this we don't need to update the code if we want to add an Extra URL in future. We only need to update the Text file inside the server.Is it possible? how?

Upvotes: 1

Views: 631

Answers (2)

Ruchira Randana
Ruchira Randana

Reputation: 4179

Use Volley as described here. It's the recommended approach.

Upvotes: 0

Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

You can use this code

 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("");

Upvotes: 1

Related Questions