0

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.
yoshi24
  • 3,147
  • 8
  • 45
  • 62

2 Answers2

3

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.

mportuesisf
  • 5,587
  • 2
  • 33
  • 26
  • Correct, but here is going another thing. Firstly, NPE is thrown because of the doc null object, and so runtime exception is thrown form the AsyncTask – Nikola Despotoski Aug 16 '11 at 23:34
1

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()
      } 

} 

}
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • Well ive notice that if i have good service on my debug device, it runs! But the minute i dont have service or service is EXTREMELY low like no bars, it gives the time out error – yoshi24 Aug 16 '11 at 23:31
  • That may explain why the doc is returning null – yoshi24 Aug 16 '11 at 23:33
  • Add the code inside the try catch statement. You can try looping it inside one while loop like `Document doc = null; ``while(doc==null)` `try` this `catch` that – Nikola Despotoski Aug 16 '11 at 23:36
  • Also change the `IOException e` into `Exception e`. Or add multi-catch statements for all exception you can think of that can be thrown due parsings operations. – Nikola Despotoski Aug 16 '11 at 23:37
  • couldnt i do if(doc == null) ? – yoshi24 Aug 16 '11 at 23:42
  • Yes, after you do Jsoup.connect(url).get(), you can check for doc==null, perfectly reasonable. – Nikola Despotoski Aug 16 '11 at 23:49
  • Okay cool! Sounds good! http://stackoverflow.com/questions/7086300/select-elements-causing-force-close Is another question i had, i think you can help with it. Also ill comment back letting you know if what you suggested work in a few. – yoshi24 Aug 16 '11 at 23:51
  • I just tried to if(doc = null). it still gave me the sockettimeoutexception. When service wasnt good – yoshi24 Aug 17 '11 at 00:09
  • you should check if(doc!=null) //operation. meaning if the doc object is not null it means it has information you proceed with the parsings. – Nikola Despotoski Aug 17 '11 at 00:10
  • ISnt that the same thing if(doc == null){ else{? – yoshi24 Aug 17 '11 at 00:40
  • If you do the parsing in the else statement then it is :D – Nikola Despotoski Aug 17 '11 at 00:43
  • Mate, the thing is Socket exception will be thrown on and on as long as you dont catch it and fix the issue with the server or your connection. you can catch it like try { } catch(SocketTimeoutException e) { e.printStrackTrace(); } – Nikola Despotoski Aug 17 '11 at 01:18
  • http://stackoverflow.com/questions/7086934/parsing-a-tag-with-jsoup-force-closing-for-nullpointerexception – yoshi24 Aug 17 '11 at 01:31