How do I get the content of a text that is in the link (URL) and load it in TextView? [closed]

-1

I need a way to load through a URL a text that is in it. The text is large, and this way the application would be lighter. I want to set the text that is downloaded from this link in TextView .

    
asked by anonymous 12.02.2017 / 16:35

2 answers

1

To download the text, try this:

public static String download(String url) throws IOException {
    try {
        return download(new URL(url));
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException(e);
    }
}

public static String download(URL url) throws IOException {
    ByteArrayOutputStream baixado = new ByteArrayOutputStream(20480);
    try (InputStream is = url.openStream()) {
        int t;
        while ((t = is.read()) != -1) {
            baixado.write(t);
        }
        return baixado.toString();
    }
}

private static String FURIA_DE_TITANS_URL = "https://example.com/furiadetitans";

Then you just have to do this:

seuTextView.setText(download(FURIA_DE_TITANS_URL));

However, I do not know if this is the best idea for you. Unless the amount of text is too large or change too often, consuming an internet connection does not seem like a good idea to me, and it would be easier if the texts were within your application.

    
12.02.2017 / 19:21
0

I'm not sure if it's possible to do this with a text file directly from Google Drive, because actually when you click the comment link for your .txt , it opens not only text but a preview manager of texts.

But by answering your question, you can list data from a .txt using AsyncTask . Then I created a .txt file in which you can see it as text when it is opened by the browser to exemplify it.

Basic example of using AsyncTask :

 new AsyncTask<Void,String,String>(){
        @Override
        protected String doInBackground(Void... params) {
            return toString();
        }
        protected void onPostExecute(String results){
        }
    }.execute();

Reading URL file:

URL url = new URL(url_desejada);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in .readLine()) != null) {

} in.close();

Complete code:

new AsyncTask < Void, String, String > () {

    // variavel criada para receber o texto da url
    StringBuilder text = new StringBuilder();
    @Override
    protected String doInBackground(Void...params) {
        try {
            // Cria uma URL com o arquivo desejado
            URL url = new URL("https://raw.githubusercontent.com/cleidimarviana/SOpt/master/TEXT/loremipsum.txt");

            // Ler o texto retornado pelo servidor
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            while ((str = in .readLine()) != null) {
                // str se refere a linha do texto retornado pelo servido
                // no entanto o usso do append eh para concatenar todas as linas
                text.append(str);
            } in .close();
        } catch (IOException e) {
            // aqui pode ser inserido uma alerta referente a comunicao
        }
        return text.toString();
    }
    protected void onPostExecute(String results) {
        // result é a resposta recebida pelo doInBackGround()
        // entao a linha abaixo define o textview com o resultado esperado
        textview.setText(results);
    }
}.execute();

To register, I've created a file named GetTextFileByURL in Github.     

13.02.2017 / 02:24