【问题标题】:How to parse data from a website using Jsoup in android如何在android中使用Jsoup解析来自网站的数据
【发布时间】:2014-06-29 17:00:45
【问题描述】:

我正在开发一个应用程序,我希望将来自网站的一些数据显示在活动中。为此,我使用 Jsoup 来解析数据。但我在以下位置遇到错误:

org.jsoup.nodes.Document document = Jsoup.connect(url).get();

这是我下面的完整代码,我不知道我在做什么错......

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.DocumentsContract.Document;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class Events extends Activity 
{
    //WebView web1;
    TextView t1;
    String url="https://sites.google.com/site/holyfamilychurchpestomsagar/notices-for-the-week";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_events);

        t1=(TextView)findViewById(R.id.textView1);
        Title t2=new Title();
        t2.execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.events, menu);
        return true;
    }

    private class Title extends AsyncTask<Void, Void, Void> {
        Elements title;
        String desc;

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute(); 
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                // Connect to the web site
                org.jsoup.nodes.Document document = Jsoup.connect(url).get();
                // Get the html document title
                title = document.select("meta[name=title]");
                desc = title.attr("content");

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result)
        {
            // Set title into TextView
            t1.setText(desc);
        }
    }

}

【问题讨论】:

    标签: android parsing jsoup


    【解决方案1】:

    设备在线吗?好的,我确定你想到了 ;-) 你是 确定它正是这条线?你是在你的 catch 子句中运行,还是 应用会崩溃吗?

    在使用 jsoup 时,您应该始终添加一个空指针检查, because when the selecting process is unsuccessful, the method will 返回空!

    你能发布你的堆栈跟踪吗?

    好的,当应用程序崩溃时,我很确定它不是连接本身,而是下面的行之一。添加空指针检查,或添加 catch 子句。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多