Closed Eljah closed 7 years ago
The exception has nothing to do with your code, it happens earlier - while parsing the XML. It says that the XML returned by the API (you use) is invalid.
It would be helpful to see what call you actually make with your mapDataHandler.
Ok, the call occurs in this cli programm:
import de.westnordost.osmapi.OsmConnection; import de.westnordost.osmapi.map.MapDataDao; import de.westnordost.osmapi.map.data.*; import de.westnordost.osmapi.map.handler.MapDataHandler; import org.fit.cssbox.demo.PdfImageRenderer; import org.xml.sax.SAXException;
import java.awt.; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Member; import java.util.; import java.util.List;
/**
Created by eljah32 on 9/21/2017. */ public class ConnectionTester { public static void main(String[] args) { OsmConnection osm = new OsmConnection( "http://api.openstreetmap.org/api/0.6/", "BusRouteDataExtractor 1.0", null);
MapDataDao mapDao = new MapDataDao(osm);
double LONGITUDE_START = 48.833744;
double LATITUDE_START = 55.693307;
double LONGITUDE_STOP = 49.261698;
double LATITUDE_STOP = 55.897801;
double currentLong = LONGITUDE_START;
double currentLat = LATITUDE_START;
final HashMap<String, Relation> busRoutes = new HashMap<String, Relation>();
MapDataHandler mapDataHandler = new MapDataHandler() {
public void handle(BoundingBox boundingBox) {
}
public void handle(Node node) {
if (node.getTags().get("highway").equals("bus_stop")) {
System.out.println(node.getTags().get("name"));
System.out.println(node.getTags().get("name:tt"));
System.out.println(node.getTags().get("name:ru"));
System.out.println(node.getTags().get("name:en"));
}
}
public void handle(Way way) {
}
public void handle(Relation relation) {
if (relation.getTags().get("type").equals("route")) {
if (relation.getTags().get("route").equals("bus")) {
System.out.println(relation.getTags().get("name"));
busRoutes.put(relation.getTags().get("name"), relation);
}
}
}
};
OsmLatLon min = null;
OsmLatLon max = null;
while (currentLat < LATITUDE_STOP) {
while (currentLong < LONGITUDE_STOP) {
//System.out.println(currentLat+", "+currentLong);
min = new OsmLatLon(currentLat, currentLong);
max = new OsmLatLon(currentLat + 0.02, currentLong + 0.02);
BoundingBox kazanBuses = new BoundingBox(min, max);
try {
mapDao.getMap(kazanBuses, mapDataHandler);
} catch (de.westnordost.osmapi.common.errors.OsmApiReadResponseException e) {
System.out.println(e.toString());
} catch (de.westnordost.osmapi.common.errors.OsmConnectionException e) {
try {
Thread.sleep(10000);
//currentLong = currentLong -0.2;
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println(e.toString());
}
currentLong = currentLong + 0.02;
}
currentLong = LONGITUDE_START;
currentLat = currentLat + 0.02;
}
Iterator it = busRoutes.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pair = (Map.Entry) it.next();
System.out.println(pair.getKey() + " = " + ((Relation)pair.getValue()).getId());
it.remove(); // avoids a ConcurrentModificationException
}
Relation bus=mapDao.getRelation(1282305);
//bus.g
for (RelationMember member: bus.getMembers()) {
if (member.getType().equals(Element.Type.NODE))
{
//System.out.println(member.getRef());
Node stop=mapDao.getNode(member.getRef());
System.out.print(stop.getTags().get("name"));System.out.print(stop.getTags().get("name:tt"));System.out.println(stop.getTags().get("name:en"));
}
/*
List<Node> routeNodes=new ArrayList<Node>();
if (member.getType().equals(Element.Type.WAY))
{
//System.out.println(member.getRef());
Way line=mapDao.getWay(member.getRef());
for (Long id: line.getNodeIds()) {
routeNodes.add(mapDao.getNode(id));
}
}
for (Node id: routeNodes) {
System.out.println(id.getPosition().getLatitude()+" "+id.getPosition().getLongitude());;
}
*/
}
} }
and on public void handle(Node node) { if (node.getTags().get("highway").equals("bus_stop
I'm getting de.westnordost.osmapi.common.errors.OsmApiReadResponseException: de.westnordost.osmapi.common.errors.XmlParserException: Error parsing XML at END_TAG @4:164 in java.io.InputStreamReader@5dc21014 and so on
I did not ask for your source code. I asked for what HTTP call you actually make.
OsmConnection line 267: What URL cannot be parsed?
By the way, you should really consider using Overpass for things like that. Overpass answers are the same format as OSM Api answers, so you can still use this library for that.
Oh man, what a waste of time. I copy-pasted your code into a new project and executed it, removed your "exception handling" and got the full stack trace.
Exception in thread "main" de.westnordost.osmapi.common.errors.OsmApiReadResponseException: de.westnordost.osmapi.common.errors.XmlParserException: Error parsing XML at END_TAG </node>@4:164 in java.io.InputStreamReader@26a1ab54
at de.westnordost.osmapi.OsmConnection.handleResponse(OsmConnection.java:322)
at de.westnordost.osmapi.OsmConnection.makeRequest(OsmConnection.java:191)
at de.westnordost.osmapi.OsmConnection.makeRequest(OsmConnection.java:142)
at de.westnordost.osmapi.map.MapDataDao.getMap(MapDataDao.java:194)
at ConnectionTester.main(ConnectionTester.java:65)
Caused by: de.westnordost.osmapi.common.errors.XmlParserException: Error parsing XML at END_TAG </node>@4:164 in java.io.InputStreamReader@26a1ab54
at de.westnordost.osmapi.common.XmlParser.doParse(XmlParser.java:64)
at de.westnordost.osmapi.map.MapDataParser.parse(MapDataParser.java:58)
at de.westnordost.osmapi.map.MapDataParser.parse(MapDataParser.java:20)
at de.westnordost.osmapi.OsmConnection.handleResponse(OsmConnection.java:314)
... 4 more
Caused by: java.lang.NullPointerException
at ConnectionTester$1.handle(ConnectionTester.java:36)
at de.westnordost.osmapi.map.MapDataParser.onEndElement(MapDataParser.java:152)
at de.westnordost.osmapi.common.XmlParser.doParse(XmlParser.java:49)
... 7 more
Note the cause, this is in your code. Elements that do not have tags return null for getTags().
I have a
When the osm data is processed whti this particular handler I'm getting the following error:
de.westnordost.osmapi.common.errors.OsmApiReadResponseException: de.westnordost.osmapi.common.errors.XmlParserException: Error parsing XML at END_TAG @4:164 in java.io.InputStreamReader@1a160b5e