1

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?

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
unitedartinc
  • 738
  • 1
  • 6
  • 25
  • Possible duplicate of [Reading Text File From Server on Android](http://stackoverflow.com/questions/2922210/reading-text-file-from-server-on-android) – Abhay Shukla Feb 14 '16 at 12:32

2 Answers2

1

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("");
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
0

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

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24