我是android新手,这是我第一次使用JSoup和Async任务。我想通过这个网站(aviationweather.gov)在我的应用程序中获得航空天气预报。
我试图用Jsoup解析的表数据如下所示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<HTML>
<HEAD>
<TITLE>ADDS - TAFS form results</TITLE>
<LINK REL="StyleSheet" type="text/css" HREF="/layout/awc/mystyle.css">
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<TABLE SUMMARY="This table is used for formatting purposes only" BORDER=0 CELLPADDING=3 CELLSPACING=0>
<TR VALIGN="top">
<TD ALIGN="left" COLSPAN="2">
<H2>Aviation Digital Data Service (ADDS)</H2>
Output produced by TAFs form (1417 UTC 22 November 2013)<BR>
found at <A HREF="http://www.aviationweather.gov/adds/tafs/"> http://www.aviationweather.gov/adds/tafs/</A><BR> <BR>
</TD>
</TR>
<TR VALIGN="top">
<TD ALIGN="left" COLSPAN="2">
<PRE><font face="Monospace,Courier" size="+1">TAF EGXC 221321Z 2215/2224 01010KT 9999 SCT035
PROB30
TEMPO 2216/2224 BKN020
</font></PRE>
<PRE><font face="Monospace,Courier" size="+1">TAF EGOV 221323Z 2215/2218 01008KT 9999 FEW020
</font></PRE>
<PRE><font face="Monospace,Courier" size="+1">TAF EGXE 221047Z 2212/2217 34008KT 9999 FEW020 SCT030
TEMPO 2212/2215 -SHRA SCT018
</font></PRE>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
表格的行数与用户要求的天气数量有关。
在此代码单击图像按钮时,我正在调用Async任务:
new getPage().execute();
getPage()看起来像这样:
private class getPage extends AsyncTask<String, Void, String> {
ProgressDialog prog;
protected void onPreExecute() {
prog = new ProgressDialog(Weathergrabber.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected String doInBackground(String... params) {
try {
// lets format the icaos the user is interested in
if (etICAO1.getText().toString().length() > 0) {
icaos = etICAO1.getText().toString();
}
if (etICAO1.getText().toString().length() == 0) {
// editText is empty
}
if (etICAO2.getText().toString().length() > 0) {
icaos = icaos + "+" + etICAO2.getText().toString();
}
if (etICAO2.getText().toString().length() == 0) {
// editText is empty
}
if (etICAO3.getText().toString().length() > 0) {
icaos = icaos + "+" + etICAO3.getText().toString();
}
if (etICAO3.getText().toString().length() == 0) {
// editText is empty
}
if (etICAO4.getText().toString().length() > 0) {
icaos = icaos + "+" + etICAO4.getText().toString();
}
if (etICAO4.getText().toString().length() == 0) {
// editText is empty
}
if (etICAO5.getText().toString().length() > 0) {
icaos = icaos + "+" + etICAO5.getText().toString();
}
if (etICAO5.getText().toString().length() == 0) {
// editText is empty
}
if (etICAO6.getText().toString().length() > 0) {
icaos = icaos + "+" + etICAO6.getText().toString();
}
if (etICAO6.getText().toString().length() == 0) {
// editText is empty
}
if (etICAO7.getText().toString().length() > 0) {
icaos = icaos + "+" + etICAO7.getText().toString();
}
if (etICAO7.getText().toString().length() == 0) {
// editText is empty
}
if (etICAO8.getText().toString().length() > 0) {
icaos = icaos + "+" + etICAO8.getText().toString();
}
if (etICAO8.getText().toString().length() == 0) {
// editText is empty
}
// now lets format the webaddress
finalWebAddress = websitePart1 + icaos + websitePart2;
// try to get the data
Toast.makeText(getApplicationContext(),
"Collecting data from " + finalWebAddress,
Toast.LENGTH_LONG).show();
Document doc;
doc = Jsoup.connect(finalWebAddress).userAgent("Mozilla").get();
Element tableElement = doc.select("tr").first();
theWeather = doc.title();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return theWeather;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tvWeather.setText(result);
}
}
我希望textview tvWeather会将表数据添加到其中,但是应用程序崩溃了一个logcat:
11-22 14:46:43.100: E/AndroidRuntime(2507): FATAL EXCEPTION: AsyncTask #1
11-22 14:46:43.100: E/AndroidRuntime(2507): java.lang.RuntimeException: An error occured while executing doInBackground()
11-22 14:46:43.100: E/AndroidRuntime(2507): at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-22 14:46:43.100: E/AndroidRuntime(2507): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-22 14:46:43.100: E/AndroidRuntime(2507): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-22 14:46:43.100: E/AndroidRuntime(2507): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-22 14:46:43.100: E/AndroidRuntime(2507): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-22 14:46:43.100: E/AndroidRuntime(2507): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-22 14:46:43.100: E/AndroidRuntime(2507): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-22 14:46:43.100: E/AndroidRuntime(2507): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-22 14:46:43.100: E/AndroidRuntime(2507): at java.lang.Thread.run(Thread.java:856)
11-22 14:46:43.100: E/AndroidRuntime(2507): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-22 14:46:43.100: E/AndroidRuntime(2507): at android.os.Handler.<init>(Handler.java:121)
11-22 14:46:43.100: E/AndroidRuntime(2507): at android.widget.Toast$TN.<init>(Toast.java:361)
11-22 14:46:43.100: E/AndroidRuntime(2507): at android.widget.Toast.<init>(Toast.java:97)
11-22 14:46:43.100: E/AndroidRuntime(2507): at android.widget.Toast.makeText(Toast.java:254)
11-22 14:46:43.100: E/AndroidRuntime(2507): at com.Weathergrabber$getPage.doInBackground(Weathergrabber.java:293)
11-22 14:46:43.100: E/AndroidRuntime(2507): at com.Weathergrabber$getPage.doInBackground(Weathergrabber.java:1)
11-22 14:46:43.100: E/AndroidRuntime(2507): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-22 14:46:43.100: E/AndroidRuntime(2507): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
正如我所说,我是使用AsyncTask和Jsoup的新手,所以我不确定错误在哪里。希望我提供了足够的细节来帮助生成答案。
感谢;
安迪
答案 0 :(得分:1)
我在那里看到你的问题。您正在尝试在后台线程中制作Toast。你只能在ui线程中制作Toast。
尝试类似:
new Handler().post(new Runnable() {
@Override
public void run() {
// Code here will run in UI thread
}
});
或者您也可以使用AsyncTask的publishProgress
和onProgressUpdate
方法从ToyncTask调用toast作为进度消息。 onProgressUpdate