【问题标题】:Android: Accessing the Google Products Search within your Android-ApplicationAndroid:在您的 Android 应用程序中访问 Google Products Search
【发布时间】:2011-03-21 03:48:18
【问题描述】:

我想在我的 Android 应用程序中使用 Gdata API。更具体地说,我想让用户能够搜索关键字,使用 Gdata api 在 googleproducts 中搜索这些关键字的产品并解析我返回的 xml。

我知道如何通过 org.xml.sax.helpers.DefaultHandler 解析 xml 文件,但我想我不应该使用这样的 Handler,而是依靠 Gdata api 为我解析 xml。

我的问题是我不知道如何将 api 集成到我的应用程序中。 stackoverflow (hier) 中有一个类似的主题,但我对他们给出的答案一点也不满意。只是向某人提供“查看我们最近发布的支持 Android 的 GData Java 库的 2.1.0-alpha 版本”的信息并不能完全帮助我将 gdata 集成到我的应用程序中。

如果有人可以提供如何将 gdata api 集成到我的应用程序中的分步指南,我将不胜感激,包括用于发出搜索请求和解析来自谷歌产品的结果的代码示例。

【问题讨论】:

    标签: android google-product-search


    【解决方案1】:

    经过几天的研究,我终于找到了解决方案:

    Google 介绍了如何访问保存在 Google Base (hier) 上的项目。令人惊讶的是,您无需实现任何 Google 数据库 API 或任何东西即可访问 Google 产品,您可以通过以下方式轻松查询它们纯网址。

    您可以通过 URL 访问 Google Base 和 Google 产品中的公共项目 http://www.google.com/base/feeds/snippets 。您可以在此 URL 上附加特定查询,例如:搜索数码相机的 ?bq=digital+camera 或搜索实际 EAN 代码的 ?bq=5030932067876。

    您会返回一个包含该查询结果的 XML 文档。例如,url http://www.google.com/base/feeds/snippets?bq=5030932067876 会返回以下 XML 文档:

    <?xml version='1.0' encoding='UTF-8'?>
      <feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gm='http://base.google.com/ns-metadata/1.0' xmlns:g='http://base.google.com/ns/1.0' xmlns:batch='http://schemas.google.com/gdata/batch'>
        <id>http://www.google.com/base/feeds/snippets</id>
        <updated>2010-07-27T15:52:29.459Z</updated>
        <title type='text'>Items matching query: 5030932067876</title>
        <link rel='alternate' type='text/html' href='http://base.google.com'/>
        <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.google.com/base/feeds/snippets'/>
        <link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='http://www.google.com/base/feeds/snippets/batch'/>
        <link rel='self' type='application/atom+xml' href='http://www.google.com/base/feeds/snippets?start-index=1&amp;max-results=25&amp;bq=5030932067876'/>
        <author>
          <name>Google Inc.</name>
          <email>base@google.com</email>
        </author>
        <generator version='1.0' uri='http://base.google.com'>GoogleBase</generator>
        <openSearch:totalResults>20</openSearch:totalResults>
        <openSearch:startIndex>1</openSearch:startIndex>
        <openSearch:itemsPerPage>25</openSearch:itemsPerPage>
        <entry>
          <id>http://www.google.com/base/feeds/snippets/6567855098786723080</id>
          <published>2009-06-17T19:10:11.000Z</published>
          <updated>2010-07-26T19:36:16.000Z</updated>
          <category scheme='http://base.google.com/categories/itemtypes' term='Produkte'/>
          <title type='text'>Xb360 Fifa 09 Electronic Arts EAD07606316 5030932067876</title>
          <content type='html'>FIFA 09 Die brandneue Fußballsimulation! Geh in FIFA 09 auf den Platz und spiel professionellen Fußball, so wie du ihn dir vorstellst. Erlebe die authentischste Fußballsimulation, die EA SPORTS? je veröffentlicht hat, lebe deinen Traum vom  ...</content>
          <link rel='alternate' type='text/html' href='http://www.mercateo.com/p/615IT-R78802/Xb360_Fifa_09.html?PageID=FG-615IT-R78802'/>
          <link rel='self' type='application/atom+xml' href='http://www.google.com/base/feeds/snippets/6567855098786723080'/>
          <author>
            <name>Mercateo.com</name>
          </author>
          <g:zustand type='text'>neu</g:zustand>
          <g:mpn type='text'>EAD07606316</g:mpn>
          <g:image_link type='url'>http://images.mercateo.com/images/products/voelkner/906692_bb_00_fb.eps.jpg</g:image_link>
          <g:item_language type='text'>DE</g:item_language>
          <g:ean type='text'>5030932067876</g:ean>
          <g:id type='text'>615IT-R78802</g:id>
          <g:shipping type='shipping'>
            <g:price>4.76 eur</g:price>
          </g:shipping>
          <g:target_country type='text'>DE</g:target_country>
          <g:preis type='floatUnit'>34.14 eur</g:preis>
          <g:expiration_date type='dateTime'>2010-08-25T19:36:16Z</g:expiration_date>
          <g:marke type='text'>Electronic Arts</g:marke>
          <g:customer_id type='int'>114950</g:customer_id>
          <g:item_type type='text'>Produkte</g:item_type>
        </entry>
    

    (...更多参赛作品...)

    您只需执行以下操作即可解析此文档: 子类化 org.xml.sax.helpers.DefaultHandler。并使用以下代码初始化 myHandler(导入 javax.xml.parsers.SAXParser 和 javax.xml.parsers.SAXParserFactory 使其工作):

    MyHandler myHandler = new MyHandler();
    String urlString = "http://www.google.com/base/feeds/snippets?bq=5030932067876";
    URL link = new URL(urlString);
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();
    xr.setContentHandler(myHandler);
    InputStream stream = link.openStream();
    InputSource inputSource = new InputSource(stream);
    inputSource.setEncoding("ISO-8859-1");
    xr.parse(inputSource);
    

    根据您对 MyHandler 进行子类化的方式,对象 myHandler 应该具有您刚刚解析的所有值。

    希望它对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      相关资源
      最近更新 更多