-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 a TextView
.
-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 a TextView
.
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 it:
seuTextView.setText(download(FURIA_DE_TITANS_URL));
However, I don’t know if this is the best idea for you. Unless the amount of texts is too large or changes too often, consuming internet connection with it does not seem like a good idea, and it would be easier if the texts were within your application.
But it would not be a download but a simple upload, kind of an image that is loaded by a url connection. once the application is closed the text no longer exists and when the text is reopened and loaded again with the others.
@Moysés I don’t understand your position. If the program downloads something from the internet, even if it is during its upload, the name of this action is download.
Yes but the text file is not to be stored on the device, but only viewed as well as in browsers.
@Moysés I edited the answer. That’s what you want?
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 on the comment link referring to your .txt
, it opens not only a text, but a text visualization manager.
But by answering your question, you can list data from a . txt using a AsyncTask
. So I created a file .txt
where it is possible to view it as text when opened by browser to exemplify.
Basic example of the use of AsyncTask
:
new AsyncTask<Void,String,String>(){
@Override
protected String doInBackground(Void... params) {
return toString();
}
protected void onPostExecute(String results){
}
}.execute();
Reading the 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 leave registered, I created a file with the name Gettextfilebyurl on Github.
Browser other questions tagged java android
You are not signed in. Login or sign up in order to post.
You need to explain more about what you want. What do you mean by "(...) loading a text URL (...)"? Is the html content of a page? Is the json/xml returned?
– ramaral
I want to set the text of the link in the textview, but not the link. https://drive.google.com/open?id=0B_HQ8JP4ovcIbVcxQ3RvcDFtWHc that text in the example application.
– Moysés