Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract text from php using Jsoup result an empty textView

Tags:

android

jsoup

I am parsing this page : http://www.catedralaltapatagonia.com/invierno/partediario.php?default_tab=0

I need the weather report and the last update date and time (I read the source code,and the info is there under div#meteo_contenedor_avalanchas) but when i run the project i receive an empty textview.

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();
    }
   }
  }

The 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)

           

like image 883
Agustin Scalisi Avatar asked Mar 20 '26 13:03

Agustin Scalisi


2 Answers

Looking a bit through the code of the website, this seems get me the last 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 the 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);

Prints:

RIESGO DE AVALANCHA: 2- MODERADO

Your problem is probably that you are trying to set a HTML table to your TextView, as your code does give a html table with quite a lot of extra stuff, and I don't think 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

like image 78
JonasCz Avatar answered Mar 22 '26 09:03

JonasCz


I resolve it thanks to other post, the solution is on the way I select the Elements using the 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 the answer to my other post he resolve it https://stackoverflow.com/a/30680629/4178519

like image 41
Agustin Scalisi Avatar answered Mar 22 '26 09:03

Agustin Scalisi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!