Extracting text from php using Jsoup results in an empty textView
I am parsing this page: http://www.catedralaltapatagonia.com/invierno/partediario.php?default_tab=0
I need a weather report and the date and time of the last update (I read the source code and the information is under there div#meteo_contenedor_avalanchas
), but when I run the project, I get empty text.
This is my code:
public class Metreologia extends Activity {
public Metreologia(){}
String url = "http://www.catedralaltapatagonia.com/invierno/partediario.php";
ProgressDialog mProgressDialog;
public TextView avisostext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.metereologia);
new Title().execute();
}
// Title AsyncTask
private class Title extends AsyncTask<Void, Void, Void> {
String text;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(Metreologia.this);
mProgressDialog.setTitle("Catedral Alta Patagonia");
mProgressDialog.setMessage("loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(url).get();
Element div = document.select("div#meteo_contenedor_avalanchas").first();
text = div.text();
System.out.println(text);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
TextView avisostext = (TextView) findViewById(R.id.ultactmetero);
avisostext.setText(text);
mProgressDialog.dismiss();
}
}
}
Logcat
06-04 11:28:04.522 3503-3536/info.blacktrail.catedral E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: info.blacktrail.catedral, PID: 3503
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.jsoup.nodes.Element.text()' on a null object reference
at info.blacktrail.catedral.Metreologia$Title.doInBackground(Metreologia.java:63)
at info.blacktrail.catedral.Metreologia$Title.doInBackground(Metreologia.java:42)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
source to share
I resolve this, thanks to another post, the solution is along the way I select Items with devtools (this time with FF)
Document document = Jsoup.connect(url)
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0")
.get();
Elements ultact=document.select("#pd_foto_fondo > div:nth-child(2)");
String ultactt=ultact.text();
ultimaact=ultactt;
Thanks to https://stackoverflow.com/users/3426328/tdg with an answer to my other post it resolved it fooobar.com/questions/2230922 / ...
source to share
Looking through the website code, this seems to give me the latest update time:
String url = "http://www.catedralaltapatagonia.com/invierno/partediario.php?default_tab=0";
Document document = Jsoup.connect(url).get();
Element div = document.select("div#meteo_contenedor_avalanchas").first();
String text = div.text();
System.out.println(text);
Prints:
ÚLTIMA ACTUALIZACIÓN PARTE DIARIO: FECHA: 03 de Junio de 2015 HORA: 09:00 hs
... And this gives a weather report:
String url = "http://www.catedralaltapatagonia.com/invierno/partediario.php?default_tab=0";
Document document = Jsoup.connect(url).get();
Element div = document.select("div#meteo_avalancha").first();
String text = div.text();
System.out.println(text);
Printing
RIESGO DE AVALANCHA: 2- MODERADO
Your problem is probably that you are trying to set up an HTML table on your TextView as your code is giving an html table with a lot of unnecessary stuff and I don't think the textview supports html tables. Try the code above and let me now if it works.
Also try this URL for a simple weather forecast: http://es.snow-forecast.com/resorts/Catedral/forecasts/feed/mid/m
source to share