当前位置: 首页 > 知识库问答 >
问题:

Android Maps Api v2 XML解析问题

巫马山
2023-03-14

我一直试图解析一个由google自动生成的XML(http://maps.googleapis.com)。

这正是我在程序中用作示例的XML:

public class MapDirection {
public final static String MODE_DRIVING = "driving";
public final static String MODE_WALKING = "walking";

public MapDirection() { }

public Document getDocument(LatLng start, LatLng end, String mode) {
    try {
        URL url = new URL("http://maps.googleapis.com/maps/api/directions/xml?" 
            + "origin=" + start.latitude + "," + start.longitude  
            + "&destination=" + end.latitude + "," + end.longitude 
            + "&sensor=false&units=metric&mode=driving");
        DocumentBuilderFactory mDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder mDocumentBuilder = mDocumentBuilderFactory.newDocumentBuilder();
        Document mDocument = mDocumentBuilder.parse(new InputSource(url.openStream()));
        mDocument.getDocumentElement().normalize();
        return mDocument;
    }
    catch (Exception e) {
        System.out.println("XML Parsing Exception = " + e);
    }

    return null;
}

public String getDurationText (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "duration"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "text"));
    return node4.getTextContent();
}

public int getDurationValue (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "duration"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "value"));
    return Integer.parseInt(node4.getTextContent());
}

public String getDistanceText (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "distance"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "text"));
    return node4.getTextContent();
}

public int getDistanceValue (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "distance"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "value"));
    return Integer.parseInt(node4.getTextContent());
}

public String getStartAddress (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "start_address"));
    return node3.getTextContent();
}

public String getEndAddress (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "end_address"));
    return node3.getTextContent();
}

public String getCopyRights (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "copyrights"));
    return node2.getTextContent();
}
private int getNodeIndex(NodeList nodeList, String nodeName) {
    for(int i = 0 ; i < nodeList.getLength() ; i++) {
        if(nodeList.item(i).getNodeName().equals(nodeName))
            return i;
    }
    return -1;
}
private void getRoute(LatLng startLocation, LatLng endLocation) {
    MapDirection mMapDirection = new MapDirection();
    Document mDocument = mMapDirection.getDocument(startLocation, endLocation, MODE_DRIVING);
    String mDuration = mMapDirection.getDurationText(mDocument);
    String mDistance = mMapDirection.getDistanceText(mDocument);
    String mStartAddress = mMapDirection.getStartAddress(mDocument);
    String mEndAddress = mMapDirection.getEndAddress(mDocument);

    Toast.makeText(getBaseContext(), "Duration: " + mDuration +
                                     "Distance: " + mDistance +
                                     "Start: " + mStartAddress +
                                     "End: " + mEndAddress, Toast.LENGTH_LONG).show();

}

我的最后一个问题是,我做错了什么?我得到的错误:

09-13 10:52:57.791: E/AndroidRuntime(13720): FATAL EXCEPTION: main
09-13 10:52:57.791: E/AndroidRuntime(13720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raulbutuc.taxifinder/com.raulbutuc.menu.items.OpenMap}: java.lang.NullPointerException
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.os.Looper.loop(Looper.java:137)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.main(ActivityThread.java:4931)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at java.lang.reflect.Method.invokeNative(Native Method)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at java.lang.reflect.Method.invoke(Method.java:511)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at dalvik.system.NativeStart.main(Native Method)
09-13 10:52:57.791: E/AndroidRuntime(13720): Caused by: java.lang.NullPointerException
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.GPSMapping.MapDirection.getDurationText(MapDirection.java:63)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.menu.items.OpenMap.getRoute(OpenMap.java:170)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.menu.items.OpenMap.onCreate(OpenMap.java:163)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.Activity.performCreate(Activity.java:5008)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
09-13 10:52:57.791: E/AndroidRuntime(13720):    ... 11 more

共有1个答案

谢善
2023-03-14

我也有同样的问题。解决方案是将getDocument方法(以及所有相应的方法)转换为static。

 类似资料:
  • 问题内容: 我正在尝试在我的android应用程序中解析Json的链接是https://www.buzzador.com/apps/present_software/webservice/index.php?op=ProductQ&campaign_id=607&userid=10776 当我将其放入Json对象时,它给我带来错误错误是:08-31 14:40:52.281:WARN / Syst

  • 我花了相当多的时间阅读GraphQL教程,但不幸的是,它们似乎没有涵盖足够的深度,我无法理解。我真的很感谢你对这个现实世界的例子的帮助。 在示例中,查询位于解析器对象的根;我可以让它在单级查询中正常工作。但是,当我尝试解析嵌套查询时,嵌套解析程序从未被调用。让我非常困惑的是,我发现graphql网站上没有发布的每一个教程都放在一个查询对象中,并将它们的查询嵌套在下面,而不是根级别。 考虑以下模式:

  • val homeFeed=gson.fromjson(正文,列表::class.java) 我的代码:

  • 我正在使用Jsoup解析短html文档,该文档包含对结果进行某些逻辑操作所需的一些自定义标记 这样地: 在这种情况下,内部内容似乎丢失了,outerHtml()方法显示如下: 但是如果“show if”标签包含一个简单的文本,比如你好,它就会像预期的那样工作。 有什么想法吗?谢谢你。

  • 问题内容: 这段代码在Swift 1.1中运行良好…只是试图找出1.2中所做的更改以使其不兼容: 它给了我错误: 无法使用类型为’(String,block:(PFObject !, NSError)- > Void)的参数列表调用’GetObjectInBackgroundWithId’ 有任何想法吗?谢谢! 问题答案: 现在,使用Swift 1.2时,您应该更加谨慎地展开可选项。因此,在具有和

  • 首先,根据句子的大小,解析在小的句子集上平稳运行--从200ms到1s的顺序。 我想实现什么? 我想在1-2小时内解析50L个句子。 不知怎么的,我需要把这个转换成-> 转换为多线程调用。我编写了一个多线程执行器来做这件事,它看起来像这样-> 有什么办法可以做到吗?我能回答先前提出的问题,但没有什么好处。

  • 我对ANTLR相对来说是新的,所以请原谅我。 但是当我试图解析下面的表达式时 我最终出现以下错误: 第1:38行:'''处的令牌识别错误 第1:42行:'''处的令牌识别错误 规则r没有方法或者它有参数 规则'r'的意思是什么?我怎么能理解问题的原因呢?任何帮助都将不胜感激!

  • 问题内容: 我正在用Python阅读JSON文件,其中包含许多字段和值(约8000条记录)。Env:Windows 10,Python 3.6.4;码: 这样我得到一个错误。下面是堆栈跟踪: 伴随着我,我尝试了 与此相关,我的程序运行了很长时间,然后挂起,没有任何输出。 我搜索了几乎与此相关的所有主题,但找不到解决方案。 注意:JSON数据是有效的,因为当我在Postman /任何REST客户端上