2
I need a direction.
What method to "download" a page’s html (text only) for example: https://pt.wikipedia.org/wiki/Tancredo_Neves and display it in a textView?
2
I need a direction.
What method to "download" a page’s html (text only) for example: https://pt.wikipedia.org/wiki/Tancredo_Neves and display it in a textView?
1
Taking the example url into consideration, we can do it as follows:
Analyzing the page, I considered that the content is within a DIV
with the id mw-content-text
.
So let’s download the page, through the library Jsoup let’s find this DIV
and pick up the text
Follow an example:
Add library (build.Radle(app)):
implementation 'org.jsoup:jsoup:1.11.2'
Add permission (Androidmanifest.xml):
<uses-permission android:name="android.permission.INTERNET"/>
Mainactivity.java
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = TextView.class.cast(findViewById(R.id.txtView));
new Dowload().execute("https://pt.wikipedia.org/wiki/Tancredo_Neves");
}
class Dowload extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... strings) {
/**
* Pegamos o primeiro item da lista....
*/
final String txtUrl = strings[0];
try{
/**
* Criamos a URL
*/
final URL url = new URL(txtUrl);
/**
* Criamos a conexão com a URL
*/
HttpURLConnection con = HttpURLConnection.class.cast(url.openConnection());
/**
* Infromamos o método de requisição
*/
con.setRequestMethod("GET");
/**
* Se o código de resposta for diferente de OK (200)
* Então retornamos null;
*/
if(HttpURLConnection.HTTP_OK != con.getResponseCode()){
return null;
}
/**
* Vamos copiar o conteúdo do response....
*/
final BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
final StringBuffer buffer = new StringBuffer();
while( (line = reader.readLine()) != null ){
buffer.append(line);
}
/**
* Vamos transformar a String contendo o html em um objeto Document do Jsoup
*/
final Document document = Jsoup.parse(buffer.toString());
/**
* Vamos procurar as DIV's que tenham id (#) igual a mw-content-text
*/
final Elements divs = document.select("div#mw-content-text");
/**
* Se nulo, ou vazio, não encontrou, então retorna nulo!
*/
if(null == divs || divs.size() == 0 ){
return null;
}
/**
* pegamos o primeiro elemento
*/
final Element div = divs.first();
/**
* Pegamos o texto deste elemento
*/
final String txt = div.text();
return txt;
}catch (final Exception e){
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String s) {
if(null != s){
txtView.setText(s);
}else{
txtView.setText("Não foi possível carregar o conteúdo!");
}
}
}
}
That’s right, man, thank you very much!
Browser other questions tagged html android parser
You are not signed in. Login or sign up in order to post.
When you say, "download" a page’s html, want it all? or just the relevant content? (About Tancredo)
– Thiago Luiz Domacoski
@Thiagoluizdomacoski only the relevant content
– Junior Klawa