我正在处理一个项目,在该项目中我正在解析来自 Internet 的 XML 文件并在 TextView 中显示其内容。
过程:当我单击按钮时,我将自己转发到从 Internet 获取 xml 的预测类,但现在我将 Async Progress 对话框添加到其中。编码没有错误,但是当我在其中运行应用程序时模拟器显示捕获异常消息“错误”它没有显示在添加异步进度对话框之前显示的“信息”。
public class Prediction extends Activity{
static final String baseUrl = "http://astroyogi.in/testandroid/Tarot/Tarot_Android.xml";
TextView tv1;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.prediction);
grabURL("http://astroyogi.in/testandroid/Tarot/Tarot_Android.xml");
tv1 = (TextView) findViewById(R.id.tv1);
}
public void grabURL(String url) {
new GrabURL().execute(url);
}
private class GrabURL extends AsyncTask<String, Void, Void> {
private ProgressDialog Dialog = new ProgressDialog(Prediction.this);
static final String baseUrl = "http://astroyogi.in/testandroid/Tarot/Tarot_Android.xml";
protected void onPreExecute() {
Dialog.setMessage("Downloading source..");
Dialog.show();
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
try{
URL website = new URL(baseUrl);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
Handlingpastxml doingWork = new Handlingpastxml();
xr.setContentHandler(doingWork);
xr.parse(new InputSource(website.openStream()));
String information = doingWork.getInformation();
tv1.setText(information);
}
catch(Exception e){
tv1.setText(e.getMessage());
}
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss();
}
}
}