如何解决android中的SaxParserException
我正在使用以下代码,但得到了saxparserexception。如何解决。当我使用以下代码时,它将给出以下异常 org.xml.sax.SAXParseException:unterminated entity ref(在java.io中的位置:entity_ref&@1:799)。InputStreamReader@406cdeb0) 如何解决它请有人帮助我如何解决android中的SaxParserException,android,android-mapview,Android,Android Mapview,我正在使用以下代码,但得到了saxparserexception。如何解决。当我使用以下代码时,它将给出以下异常 org.xml.sax.SAXParseException:unterminated entity ref(在java.io中的位置:entity_ref&@1:799)。InputStreamReader@406cdeb0) 如何解决它请有人帮助我 *public class MapViewActivity extends MapActivity { MapView mapView
*public class MapViewActivity extends MapActivity
{
MapView mapView;
@Override
protected void onCreate(Bundle savedInsatnceState)
{
super.onCreate(savedInsatnceState);
setContentView(R.layout.map);
mapView = (MapView) findViewById(R.id.myMapView1);
//mapView.setBuiltInZoomControls(true);
double src_lat = 18.556019;
double src_long = 73.822283;
double dest_lat = 18.500487;
double dest_long = 73.866900;
GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),
(int) (src_long * 1E6));
GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
(int) (dest_long * 1E6));
DrawPath(srcGeoPoint, destGeoPoint, Color.GREEN, mapView);
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(15);
}
protected boolean isRouteDisplayed()
{
return false;
}
private void DrawPath(GeoPoint src, GeoPoint dest, int color,
MapView mMapView01) {
System.out.println("in drawPath");
// connect to map web service
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");// from
urlString.append(Double.toString((double) src.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString
.append(Double.toString((double) src.getLongitudeE6() / 1.0E6));
urlString.append("&daddr=");// to
urlString
.append(Double.toString((double) dest.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString.append(Double
.toString((double) dest.getLongitudeE6() / 1.0E6));
urlString.append("&ie=UTF8&0&om=0&output=kml");
Log.d("xxx", "URL=" + urlString.toString());
// get the kml (XML) doc. And parse it to get the coordinates(direction
// route).
Document doc = null;
HttpURLConnection urlConnection = null;
URL url = null;
try {
url = new URL(urlString.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(urlConnection.getInputStream());
if (doc.getElementsByTagName("GeometryCollection").getLength() > 0) {
// String path =
// doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
String path = doc.getElementsByTagName("GeometryCollection")
.item(0).getFirstChild().getFirstChild()
.getFirstChild().getNodeValue();
Log.d("xxx", "path=" + path);
String[] pairs = path.split(" ");
String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude
// lngLat[1]=latitude
// lngLat[2]=height
// src
GeoPoint startGP = new GeoPoint((int) (Double
.parseDouble(lngLat[1]) * 1E6), (int) (Double
.parseDouble(lngLat[0]) * 1E6));
mMapView01.getOverlays()
.add(new MyOverlay(startGP, startGP, 1));
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++) // the last one would be
// crash
{
System.out.println("in while");
lngLat = pairs[i].split(",");
gp1 = gp2;
// watch out! For GeoPoint, first:latitude, second:longitude
gp2 = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
mMapView01.getOverlays().add(
new MyOverlay(gp1, gp2, 2, color));
Log.d("xxx", "pair:" + pairs[i]);
}
mMapView01.getOverlays().add(new MyOverlay(dest, dest, 3)); // use
// the
// default
// color
}
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
*公共类MapViewActivity扩展了MapActivity
{
地图视图;
@凌驾
创建时受保护的void(Bundle savedinsatenceState)
{
super.onCreate(savedInsatnceState);
setContentView(R.layout.map);
mapView=(mapView)findViewById(R.id.myMapView1);
//mapView.SetBuilTinZoomControl(真);
双src_lat=18.556019;
双src_long=73.822283;
双目的地纬度=18.500487;
双目标长=73.866900;
地质点SRCEOPINT=新的地质点((int)(src_lat*1E6),
(int)(src_long*1E6);;
地质点destGeoPoint=新的地质点((int)(dest_lat*1E6),
(国际)(目的地长*1E6));
绘图路径(srcGeoPoint、destGeoPoint、Color.GREEN、mapView);
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(15);
}
受保护的布尔值isRouteDisplayed()
{
返回false;
}
专用空心绘制路径(地质点src、地质点dest、int color、,
地图视图(mMapView01){
System.out.println(“绘图路径中”);
//连接到地图web服务
StringBuilder urlString=新的StringBuilder();
urlString.append(“http://maps.google.com/maps?f=d&hl=en");
urlString.append(“&saddr=”);//来自
append(Double.toString((Double)src.getLatitudeE6()/1.0E6));
urlString.append(“,”);
URL字符串
.append(Double.toString((Double)src.getLongitudeE6()/1.0E6));
urlString.append(“&daddr=”);//到
URL字符串
.append(Double.toString((Double)dest.getLatitudeE6()/1.0E6));
urlString.append(“,”);
追加(双精度)
.toString((双)dest.getLongitudeE6()/1.0E6));
追加(“&ie=UTF8&0&om=0&output=kml”);
Log.d(“xxx”,“URL=“+urlString.toString());
//获取kml(XML)文档,并对其进行解析以获得坐标(方向)
//路线)。
单据单据=空;
HttpURLConnection-urlConnection=null;
URL=null;
试一试{
url=新url(urlString.toString());
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod(“GET”);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
doc=db.parse(urlConnection.getInputStream());
if(doc.getElementsByTagName(“GeometryCollection”).getLength()>0){
//字符串路径=
//doc.getElementsByTagName(“GeometryCollection”).item(0.getFirstChild().getFirstChild().getNodeName();
字符串路径=doc.getElementsByTagName(“GeometryCollection”)
.item(0).getFirstChild().getFirstChild()
.getFirstChild().getNodeValue();
Log.d(“xxx”,“path=“+path”);
String[]pairs=path.split(“”);
字符串[]lngLat=pairs[0]。拆分(“,”;//lngLat[0]=经度
//lngLat[1]=纬度
//lngLat[2]=高度
//src
地质点startGP=新的地质点((int)(双精度)
.parseDouble(lngLat[1])*1E6,(int)(双精度)
.parseDouble(lngLat[0])*1E6);
mMapView01.getOverlays()
.添加(新MyOverlay(startGP,startGP,1));
地质点gp1;
地质点gp2=startGP;
对于(int i=1;i
}*您需要传递url而不是url连接,然后在其上使用openStream:
doc = db.parse(url.openStream());
问题可能与删除旧的kml数据以从Google检索地图信息有关
可能XML中有错误,您应该检查它。当我在浏览器中给出url时,它将给出两点之间的正确位置。但在doc=db.parse(urlConnection.getInputStream())行给出异常;