如何使用的AsyncTask的Jsoup分析器? [英] How to use AsyncTask for Jsoup Parser?

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

问题描述

我试图用一个AsyncTask的,因为在activty力关闭,因为它需要长期的主线上。

下面是我想在一个DoInBackGround ..

要使用

 }
一流的读取器扩展的AsyncTask<虚空,虚空,虚空> {

    @覆盖
    保护无效doInBackground(虚空......为arg0){
        文档DOC = NULL;
        尝试 {
            DOC = Jsoup.connect(URL)获得();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
        TextView的文章=(TextView中)findViewById(R.id.releaseInfo);
        最后的TextView featuresText =(TextView中)findViewById(R.id.features);

        //获取概述DIV
        。元素概述= doc.select(#DIV对象概述)去年();
        元featureList = doc.select(div.callout箱)最后的()。

        元素的特性= featureList.select(礼);
        的ListIterator&其中;组件> featList = features.listIterator();
        而(featList.hasNext()){
            featuresText.setText(特色:+ featList.next()文本()+\ N);

        }


        //获取段落元素
        。元款= overview.select(P)去年();
        的System.out.println(paragraph.text());

        article.setText(paragraph.text());


        返回null;
    }

}
 

}

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

编辑:这是调试错误的,我得到

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

更多...

  08-16 18:52:59.577:ERROR / AndroidRuntime(27209):致命异常:AsyncTask的#2
 08-16 18:52:59.577:ERROR / AndroidRuntime(27209):java.lang.RuntimeException的:执行doInBackground时出错()
08-16 18:52:59.577:ERROR / AndroidRuntime(27209):在android.os.AsyncTask $ 3.done(AsyncTask.java:200)
08-16 18:52:59.577:ERROR / AndroidRuntime(27209):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:273)
08-16 18:52:59.577:ERROR / AndroidRuntime(27209):在java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-16 18:52:59.577:ERROR / AndroidRuntime(27209):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307)
08-16 18:52:59.577:ERROR / AndroidRuntime(27209):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-16 18:52:59.577:ERROR / AndroidRuntime(27209):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-16 18:52:59.577:ERROR / AndroidRuntime(27209):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:561)
08-16 18:52:59.577:ERROR / AndroidRuntime(27209):在java.lang.Thread.run(Thread.java:1096)
08-16 18:52:59.577:ERROR / AndroidRuntime(27209):由:显示java.lang.NullPointerException
08-16 18:52:59.577:ERROR / AndroidRuntime(27209):在com.fttech.htmlParser.HtmlparserExampleActivity$getGames.doInBackground(HtmlparserExampleActivity.java:156)
08-16 18:52:59.577:ERROR / AndroidRuntime(27209):在com.fttech.htmlParser.HtmlparserExampleActivity$getGames.doInBackground(HtmlparserExampleActivity.java:1)
 

该NullPointerException异常点,这种方法在doInBackground()

 元素的游戏= doc.select(TR> td.indexList1,TR> td.indexList2);
 

编辑:在Android蜂窝运行时,我得到这个错误

  08-16 19:04:01.600:ERROR / AndroidRuntime(7302):android.view.ViewRoot $ CalledFromWrongThreadException:产生的原因只有创建视图层次结构原来的线程可以触摸其观点。
 

解决方案

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

您连接超时。这就是为什么NPE被抛出,因为你正试图。选择从一个空对象(东西)。该文档 文件对象是空的,因为没有从URL返回的数据。这样一来,的AsyncTask 实例抛出的RuntimeException

检查,如果你是落后的代理。检查您是否可以访问该网址在浏览器中。检查仿真器的DNS。同时检查是否允许互联网在你的清单。

加:更新UI必须在UI线程来完成。也就是说,你需要做的,在 onPostExecute()

Еdit:

 类读取器扩展的AsyncTask<虚空,虚空,虚空> {
   //此申报使用解析变量
   私人元素概述= NULL;
   私人元featureList = NULL;
   私人拥有的元素= NULL;
   私人元款= NULL;
    @覆盖
    保护无效doInBackground(虚空......为arg0){
        文档DOC = NULL;
        尝试 {
            DOC = Jsoup.connect(URL)获得();
      概述= doc.select(#DIV对象概述)去年();
       featureList = doc.select(div.callout箱)最后的()。

       功能= featureList.select(礼);
       ,第一款= overview.select(P)去年();
        的System.out.println(paragraph.text());
        }赶上(例外五){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

        //获取段落元素
       // article.setText(paragraph.text());我评论了这一点,因为你无法更新从非UI线程的用户界面,因为doInBackground是在后台线程中运行。


        返回null;
    }
     @覆盖
     保护无效onPostExecute(虚空结果)
     {

    TextView的文章=(TextView中)findViewById(R.id.releaseInfo);
        最后的TextView featuresText =(TextView中)findViewById(R.id.features);
      对于(元素e:功能)
      {
        //的setText()
      }

}

}
 

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

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() + "\n");

        }


        // 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?

EDIT: This is the debug error's i get

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)

More...

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)

The NullPointerException points to this method in the doInBackground()

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

EDIT: 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

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.

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.

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分析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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