I'm trying to parse some XML data from the web. Most of the time, the XML's are clean and well displayed in my app. This works perfectly. I also want to build in some security, so the app doesn't crash when there's a bad XML.
So when an XML couldn't get parsed it hops to my catch block where I proceed this:
try {
Log.e("in try", "try");
/* Create a URL we want to load some xml-data from. */
URL url = new URL("http://172.21.150.140:80/scripts/cgiip.exe/WService=brAccentBe/Android/getVacatureDetails.html?Vacid=" + vacaid + "&Kantoorid=" + kantoorid);
System.out.println("Url " + url);
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
vacatureDetailsWebservice vs = new vacatureDetailsWebservice();
xr.setContentHandler(vs);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(url.openStream()));
/* Parsing has finished. */
/* Our ExampleHandler now provides the parsed data to us. */
vaca = vs.getVacatures();
}
catch (Exception e) {
final AlertDialog.Builder builder = new AlertDialog.Builder(JobDetails.this);
builder.setTitle("Fout in XML");
builder.setMessage("Er is een fout opgetreden in de data. Probeer het later nog eens");
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
开发者_高级运维 public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
builder.create().show();
}
But the Dialog won't show, the class continues to run and gets other exceptions because XML isn't parsed. This method is called in onCreate.
Does anyone see what I'm doing wrong?
Please try below things may be it will work but not sure...
boolean bol = false;
try{
// your code
} catch (Exception e) {
bol = true;
}
if (bol){
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Fout in XML");
builder.setMessage("Er is een fout opgetreden in de data. Probeer het later nog eens");
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
builder.create().show();
}
精彩评论