【问题标题】:XmlPullParser doesn't parse all tags in Google PlacesXmlPullParser 不解析 Google Places 中的所有标签
【发布时间】:2013-06-04 12:35:42
【问题描述】:

我正在构建一个 android 应用程序,用户可以在其中根据他的搜索获取可能的城市列表。我已经使用 Google Places Api 来实现这一点,并且我正在使用 XMLPullParser 来解析收到的 xml。我尝试了以下代码

public ArrayList<String> getPlaceList(InputStream in) {
        // TODO Auto-generated method stub

        ArrayList<String> placesList = new ArrayList<String>();

        XmlPullParserFactory factory;
        try {
            factory = XmlPullParserFactory.newInstance();
            // allow xmlpullparserfactory to parse document as xml,default false
            factory.setNamespaceAware(true);
            XmlPullParser parser = factory.newPullParser();
            // assign inputstream and no encoding
            parser.setInput(in, null);

            int eventType = parser.getEventType();
            // until the document ends


            while (eventType != XmlPullParser.END_DOCUMENT) {


                Toast t = Toast.makeText( myContext, parser.getName(), Toast.LENGTH_LONG);
                t.show();

                if (eventType == XmlPullParser.START_TAG
                        && parser.getName().equals("prediction")) {

                    eventType = parser.next();
                    while (!(eventType == XmlPullParser.END_TAG
                            && parser.getName().equals("prediction"))) {

                        if (eventType == XmlPullParser.START_TAG
                                && parser.getName().equals("description")) {

                            placesList.add(parser.nextText());
                            String text="text ";

                        }
                        eventType = parser.next();
                    }

                }

                eventType=parser.next();
            }
            return placesList;
        } catch (Exception e) {

            Toast t1 = Toast.makeText( myContext, "error xmlhandling", Toast.LENGTH_LONG);
            t1.show();
        }
        return placesList;

    }

这里是 Google 地方信息

    <?xml version="1.0" encoding="UTF-8"?>
<AutocompletionResponse>
 <status>OK</status>
 <prediction>
  <description>Delhi, India</description>
  <type>locality</type>
  <type>political</type>
  <type>geocode</type>
  <reference>CjQkAAAAVhtWJ9i5zyacN4kIoR7-YN4MIVw0d14B9WIs7R4PQDPEV4B_C2lZqEPDxLlwHvn_EhCJZfrQNdx-1Bpug0gfs8j5GhSuqEbP4q_w5kniq5Hhora1pddvHg</reference>
  <id>910e9ee78403c6726e140168f78ca0fb0063bcc8</id>
  <term>
   <value>Delhi</value>
   <offset>0</offset>
  </term>
  <term>
   <value>India</value>
   <offset>7</offset>
  </term>
  <matched_substring>
   <offset>0</offset>
   <length>4</length>
  </matched_substring>
 </prediction>
 <prediction>
  <description>Delhi Cantonment, New Delhi, Delhi, India</description>
  <type>sublocality</type>
  <type>political</type>
  <type>geocode</type>
  <reference>ClRBAAAAldH2XsHxApFz9h9KT7EqFGQY9lX3fPbIQEvY2r4QX9CfMywdOa3do8D1TxG3QvPDvXaN1NrXgefVas5CEqq51WvUohlweUr9Iy3ksOXem4YSEFyeN_RIOYZJDBPnd0I3MxkaFPbTYVUqILD-SuAKFxYFBhFOMxbT</reference>
  <id>356175a156a9604b0ed46d21a809d9fa798a744c</id>
  <term>
   <value>Delhi Cantonment</value>
   <offset>0</offset>
  </term>
  <term>
   <value>New Delhi</value>
   <offset>18</offset>
  </term>
  <term>
   <value>Delhi</value>
   <offset>29</offset>
  </term>
  <term>
   <value>India</value>
   <offset>36</offset>
  </term>
  <matched_substring>
   <offset>0</offset>
   <length>4</length>
  </matched_substring>
 </prediction>
 <prediction>
  <description>Delhi, ON, Canada</description>
  <type>locality</type>
  <type>political</type>
  <type>geocode</type>
  <reference>CjQpAAAAfDmekZTvFnEEpCFLn1-hpWDij8X-bYeNzs8yA7NOnSJJ3yapT_OFPDLGi-VXSivKEhDtZcoOZp7JdYzA6JYIRll1GhQb2XZt9on9VDS7mnfBPA-8yjoYlQ</reference>
  <id>efdea5b4936bf049207428306448db6fabccaebf</id>
  <term>
   <value>Delhi</value>
   <offset>0</offset>
  </term>
  <term>
   <value>ON</value>
   <offset>7</offset>
  </term>
  <term>
   <value>Canada</value>
   <offset>11</offset>
  </term>
  <matched_substring>
   <offset>0</offset>
   <length>4</length>
  </matched_substring>
 </prediction>
 <prediction>
  <description>Delhi, CA, United States</description>
  <type>locality</type>
  <type>political</type>
  <type>geocode</type>
  <reference>CjQwAAAAzQLQe_g2_KUMEyM5kJMhOm_hdnk6qx4Djnt_yA1MbWPRQeuT-Ut9XHCOmK1MTFxuEhDqeOLsbn-TpEyAk6vGScGFGhTa6OPk0PGDfMElvpstHYqwngJ0UQ</reference>
  <id>bda0d7fb7c9c504032d72df29c79b08dba107d70</id>
  <term>
   <value>Delhi</value>
   <offset>0</offset>
  </term>
  <term>
   <value>CA</value>
   <offset>7</offset>
  </term>
  <term>
   <value>United States</value>
   <offset>11</offset>
  </term>
  <matched_substring>
   <offset>0</offset>
   <length>4</length>
  </matched_substring>
 </prediction>
 <prediction>
  <description>Delhi, NY, United States</description>
  <type>locality</type>
  <type>political</type>
  <type>geocode</type>
  <reference>CjQwAAAAq1EUig-oKcgx9zYXX9Fe3r3FYa77MlpaWFWg0gqSJy_vSgiki1Uw6s7UhTaXH1KYEhAabBkchKrn6lnpw4Wl21VHGhT9STCizr1QErgYEheGT0Dl5q2GZA</reference>
  <id>dd0ac43b6b0b05c55302ed222b8cb4d0c19d1abf</id>
  <term>
   <value>Delhi</value>
   <offset>0</offset>
  </term>
  <term>
   <value>NY</value>
   <offset>7</offset>
  </term>
  <term>
   <value>United States</value>
   <offset>11</offset>
  </term>
  <matched_substring>
   <offset>0</offset>
   <length>4</length>
  </matched_substring>
 </prediction>
</AutocompletionResponse>

Toast t 只显示AutocompletionResponsestatus 因此这意味着只有这两个标签正在被解析。这就是问题所在,所有标签都应该被解析,尤其是predictiondescription

【问题讨论】:

  • @Segi 更新了问题。我无法解析所有标签。正如您可能从代码中看到的那样,我需要解析预测和描述标签。
  • @Segi 该示例使用了我不理解的 HashMaps。我正在关注应用程序 4 android 应用程序的示例,这对初学者来说很容易。我在网上看到过类似的例子,都使用相同的方法,但我不知道为什么predictiondescription没有被解析。

标签: android xml-parsing google-places-api android-xml


【解决方案1】:

终于搞定了。我创建了一个新的Key for Android apps 并在只有AutocompletionResponsestatus 并且有Access denied 错误的URL 中使用它。对于所有遇到类似错误的人,请创建一个 Key for browser apps 并在 URL 中使用它。还要确保Places API 在以下链接中处于打开状态。 code.google.com/apis/console/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-09
    • 2013-02-13
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多