如何将 AsyncTask 用于 Jsoup Parser? [英] How to use AsyncTask for Jsoup Parser?

查看:13
本文介绍了如何将 AsyncTask 用于 Jsoup Parser?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AsyncTask,因为活动强制关闭,因为它在主线程上花费了很长时间.

I am trying to use a AsyncTask because the the activty force closes because it takes to long on the main thread.

这是我想在 DoInBackGround 中使用的内容..

Here is what i want to use in a DoInBackGround..

}
class fetcher extends AsyncTask<Void,Void, Void>{

    @Override
    protected Void doInBackground(Void... arg0) { 
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        TextView article = (TextView)findViewById(R.id.releaseInfo);
        final TextView featuresText = (TextView)findViewById(R.id.features);

        // Get the overview div
        Element overview = doc.select("div#object-overview").last();
        Element featureList = doc.select("div.callout-box").last();

        Elements features = featureList.select("li");
        ListIterator<Element> featList = features.listIterator();
        while (featList.hasNext()) {
            featuresText.setText("Features: " + featList.next().text() + "
");

        }


        // Get the paragraph element
        Element paragraph = overview.select("p").last();
        System.out.println(paragraph.text());

        article.setText(paragraph.text());


        return null;
    }

}

}

它总是强制关闭我不知道为什么?

it always force closes i dont know why?

这是我得到的调试错误

08-16 18:52:59.547: WARN/System.err(27209): java.net.SocketTimeoutException
08-16 18:52:59.547: WARN/System.err(27209):     at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:564)
08-16 18:52:59.547: WARN/System.err(27209):     at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:61)
08-16 18:52:59.547: WARN/System.err(27209):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readln(HttpURLConnectionImpl.java:1279)
08-16 18:52:59.547: WARN/System.err(27209):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readServerResponse(HttpURLConnectionImpl.java:1351)
08-16 18:52:59.547: WARN/System.err(27209):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.sendRequest(HttpURLConnectionImpl.java:1339)
08-16 18:52:59.547: WARN/System.err(27209):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1656)
08-16 18:52:59.547: WARN/System.err(27209):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
08-16 18:52:59.557: WARN/System.err(27209):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1374)

更多...

08-16 18:52:59.577: ERROR/AndroidRuntime(27209): FATAL EXCEPTION: AsyncTask #2
 08-16 18:52:59.577: ERROR/AndroidRuntime(27209): java.lang.RuntimeException: An error occured while executing doInBackground()
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):     at java.lang.Thread.run(Thread.java:1096)
08-16 18:52:59.577: ERROR/AndroidRuntime(27209): Caused by: java.lang.NullPointerException
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):     at com.fttech.htmlParser.HtmlparserExampleActivity$getGames.doInBackground(HtmlparserExampleActivity.java:156)
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):     at com.fttech.htmlParser.HtmlparserExampleActivity$getGames.doInBackground(HtmlparserExampleActivity.java:1)

在 doInBackground() 中 NullPointerException 指向这个方法

The NullPointerException points to this method in the doInBackground()

                Elements games = doc.select("tr>  td.indexList1, tr > td.indexList2");

在android蜂窝上运行时出现此错误

I get this error when running it on android honeycomb

08-16 19:04:01.600: ERROR/AndroidRuntime(7302): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

推荐答案

http://download.oracle.com/javase/1.5.0/docs/api/java/net/SocketTimeoutException.html

您的连接已超时.这就是 NPE 被抛出的原因,因为您试图从一个空对象中 .select("something") .doc Document 对象为空,因为没有从 url 返回的数据.所以 AsyncTask 实例抛出 RuntimeException.

You connection has timed out. That's why NPE is thrown since you are trying to .select("something") from a null object. The doc Document object is null, since there is no data returned from the url. And so the AsyncTask instance is throwing RuntimeException.

检查您是否支持代理.检查您是否可以在浏览器中访问该 url.检查模拟器的DNS.还要检查清单中的许可 INTERNET.

Check if you are behind proxy. Check if you can access the url in your browser. Check the DNS of the emulator. Also check for permission INTERNET in your manifest.

另外:更新 UI 必须在 UI 线程中完成.即您需要在 onPostExecute()

Plus: Updating UI must be done in UI-thread. i.e you need to do that in onPostExecute()

Еdit:

class fetcher extends AsyncTask<Void,Void, Void>{
   //HERE DECLARE THE VARIABLES YOU USE FOR PARSING
   private Element overview=null;
   private  Element featureList=null;
   private Elements features=null;
   private Element paragraph =null;
    @Override
    protected Void doInBackground(Void... arg0) { 
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
      overview = doc.select("div#object-overview").last();
       featureList = doc.select("div.callout-box").last();

       features = featureList.select("li");
       paragraph = overview.select("p").last();
        System.out.println(paragraph.text());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Get the paragraph element
       // article.setText(paragraph.text()); I comment this out because you cannot update ui from non-ui thread, since doInBackground is running on background thread.


        return null;
    }
     @Override
     protected Void onPostExecute(Void result)
     {

    TextView article = (TextView)findViewById(R.id.releaseInfo);
        final TextView featuresText = (TextView)findViewById(R.id.features);
      for(Element e: features)
      {
        //setText()
      } 

} 

}

这篇关于如何将 AsyncTask 用于 Jsoup Parser?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆