How To Use Asynctask For Jsoup Parser?
Solution 1:
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.
Solution 2:
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:
classfetcherextendsAsyncTask<Void,Void, Void>{
//HERE DECLARE THE VARIABLES YOU USE FOR PARSINGprivate Element overview=null;
private Element featureList=null;
private Elements features=null;
privateElementparagraph=null;
@Overrideprotected Void doInBackground(Void... arg0) {
Documentdoc=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.returnnull;
}
@Overrideprotected Void onPostExecute(Void result)
{
TextViewarticle= (TextView)findViewById(R.id.releaseInfo);
finalTextViewfeaturesText= (TextView)findViewById(R.id.features);
for(Element e: features)
{
//setText()
}
}
}
Post a Comment for "How To Use Asynctask For Jsoup Parser?"