Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

android - How to use AsyncTask for Jsoup Parser?

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

        }


        // 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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Another problem with your code sample: The doInBackground() is run on a separate thread. You are trying to manipulate two TextViews from the background thread, which isn't permitted in Android.

From the Android SDK docs:

Additionally, the Andoid UI toolkit is not thread-safe. So, you must not manipulate your UI from a worker thread—you must do all manipulation to your user interface from the UI thread. Thus, there are simply two rules to Android's single thread model:

Do not block the UI thread

Do not access the Android UI toolkit from outside the UI thread

You'll have to move the calls to TextView back to your main thread. AsyncTask gives you two built-in ways to do this: onProgressUpdate() and onPostExecute(). The code you put into either of those methods will get run on the main UI thread.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...