Я использую следующий код в Async-task для анализа веб-службы и отображения результатов на карте (в HTC desire s, реальном устройстве) ..
protected Void doInBackground(Void... params) {
try {
/** Handling XML */
spf = SAXParserFactory.newInstance();
sp = spf.newSAXParser();
xr = sp.getXMLReader();
String response = doGet("http://www.abc.com");
myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
//xr.parse(new InputSource(sourceUrl.openStream()));
xr.parse(new InputSource(new StringReader(response)));
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (NullPointerException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
markerssList = MyXMLHandler.markerList;
mapOverlays_results =mapView.getOverlays();
mapOverlays_results.remove(itemizedOverlay);
if(markerssList.getLatitude().size()>0)
{
Drawable drawable = HSLoc_tabActivity.this.getResources().getDrawable(R.drawable.flag);
itemizedOverlay = new CustomItemizedOverlay(drawable, HSLoc_tabActivity.this);
for (int i = 0; i < markerssList.getLatitude().size(); i++) {
ss1 = markerssList.getLatitude().get(i);
ss2 = markerssList.getLongitude().get(i);
ss3 = markerssList.getName().get(i);
ss4 = markerssList.getAddress().get(i);
ss5 = markerssList.getCity().get(i);
markerssList.getPostal_code().get(i);
try
{
p = new GeoPoint((int) (Double.parseDouble(ss1) * 1E6),
(int) (Double.parseDouble(ss2) * 1E6));
overlayItem = new OverlayItem(p, " ",
""+ss3+"\n"+ss4+"\n"+ss5);
itemizedOverlay.addOverlay(overlayItem);
}
catch(NullPointerException e1)
{
}
}
mapOverlays_results.add(itemizedOverlay);
//mapView.getOverlays().add(itemizedOverlay);
mapController = mapView.getController();
mapController.animateTo(p);
mapController.setZoom(12);
mapView.postInvalidate();
}
код работает нормально ... но при низкой силе сетевого приложения закрыто, и я получил следующую ошибку:
01-02 13:41:29.222: WARN/System.err(6562): java.net.UnknownHostException: www.abc.com.au
01-02 13:41:29.222: WARN/System.err(6562): at java.net.InetAddress.lookupHostByName(InetAddress.java:509)
01-02 13:41:29.232: WARN/System.err(6562): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:297)
01-02 13:41:29.232: WARN/System.err(6562): at java.net.InetAddress.getAllByName(InetAddress.java:256)
01-02 13:41:29.232: WARN/System.err(6562): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
01-02 13:41:29.232: WARN/System.err(6562): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-02 13:41:29.232: WARN/System.err(6562): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-02 13:41:29.232: WARN/System.err(6562): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359
)
и
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): FATAL EXCEPTION: AsyncTask #2
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): java.lang.RuntimeException: An error occured while executing doInBackground()
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): at java.lang.Thread.run(Thread.java:1027)
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): Caused by: java.lang.NullPointerException
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): at com.app.LocatorApp.HSLoc_tabActivity$backgroundLoadResults.doInBackground(HSLoc_tabActivity.java:545)
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): at com.app.LocatorApp.HSLoc_tabActivity$backgroundLoadResults.doInBackground(HSLoc_tabActivity.java:1)
Как справиться с этими ошибками?