XmlPullParser不会解析Google地方信息中的所有代码

时间:2013-06-08 07:08:56

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

我正在构建一个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

1 个答案:

答案 0 :(得分:0)

终于搞定了。我创建了一个新的Key for Android apps,并在只有AutocompletionResponsestatus并且出现Access denied错误的网址中使用了该Key for browser apps。对于所有遇到类似错误的人,请创建Places API并在URL中使用该错误。另外,请确保以下链接中已启用{{1}}。 code.google.com/apis/console/