我正在尝试使用AsyncTask,因为激活力会关闭,因为主线程需要很长时间。
以下是我想在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;
}
}
}
它总是强行关闭我不知道为什么?
编辑:这是我得到的调试错误
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)
NullPointerException指向doInBackground()
中的此方法 Elements games = doc.select("tr> td.indexList1, tr > td.indexList2");
编辑:我在android honey上运行它时出现此错误
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.
答案 0 :(得分:3)
代码示例的另一个问题:doInBackground()
在单独的线程上运行。您正试图从后台线程中操纵两个TextViews
,这在Android中是不允许的。
此外,Andoid UI工具包不是线程安全的。所以,你必须 不要从工作线程操纵你的UI - 你必须做所有 从UI线程操作到您的用户界面。因此,那里 只是Android单线程模型的两个规则:
不要阻止UI线程
不要从UI线程外部访问Android UI工具包
您必须将调用TextView
移回主线程。 AsyncTask
为您提供了两种内置方法:onProgressUpdate()
和onPostExecute()
。您放入这些方法中的代码将在主UI线程上运行。
答案 1 :(得分:1)
http://download.oracle.com/javase/1.5.0/docs/api/java/net/SocketTimeoutException.html
您的连接已超时。这就是为什么NPE被抛出,因为你试图从空对象.select("something")
。 doc
Document
对象为null,因为没有从url返回的数据。因此AsyncTask
实例正在抛出RuntimeException
。
检查您是否支持代理。检查您是否可以在浏览器中访问该网址。检查模拟器的DNS。还要检查清单中的INTERNET权限。
Plus:必须在UI线程中完成更新UI。即你需要在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()
}
}
}