使用Jsoup结果从text提取文本

时间:2015-06-03 05:22:46

标签: android jsoup

我正在解析此页面:http://www.catedralaltapatagonia.com/invierno/partediario.php?default_tab=0

我需要天气预报和上次更新日期和时间(我阅读了源代码,信息在div#meteo_contenedor_avalanchas下),但是当我运行项目时,我收到一个空的文本视图。

这是我的代码:

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)

2 个答案:

答案 0 :(得分:2)

看一下网站的代码,这似乎让我得到了最后的更新时间:

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

打印:

ÚLTIMA ACTUALIZACIÓN PARTE DIARIO: FECHA: 03 de Junio de 2015 HORA: 09:00 hs

...这给出了天气预报:

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

打印:

RIESGO DE AVALANCHA: 2- MODERADO

您的问题可能是您正在尝试将HTML表格设置为TextView,因为您的代码确实为html表提供了大量额外的内容,而且我认为textview不支持html表格。试试上面的代码吧,如果有效的话,现在就让我吧。

另外,请尝试使用此网址进行简单的天气预报:http://es.snow-forecast.com/resorts/Catedral/forecasts/feed/mid/m

答案 1 :(得分:1)

我解决了感谢其他帖子,解决方案是我使用devtools选择元素的方式(这次是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;

感谢https://stackoverflow.com/users/3426328/tdg对我的其他帖子的回答,他解决了https://stackoverflow.com/a/30680629/4178519