2010-07-26 138 views
0

我想在我的Android應用程序中使用Gdata Apis。更具體地說,我想讓用戶能夠搜索關鍵字,使用Gdata api在googleproducts中搜索這些關鍵字的產品,並解析我返回的xml。Android:在Android應用程序中訪問Google產品搜索

我知道如何通過org.xml.sax.helpers.DefaultHandler解析xml文件,但我想我不會使用這樣的Handler,而是依賴於Gdata api來爲我解析xml。

我的問題是,我不知道如何將API集成到我的應用程序中。在stackoverflow中有一個類似的主題(hier),但我完全不滿意他們給出的答案。只是給某人的信息「看看我們最近宣佈的支持Android的GData Java庫的2.1.0-alpha版本」並不能幫助我將gdata集成到我的應用程序中。

如果有人能分步指導如何將gdata api集成到我的應用程序中,包括代碼示例以製作搜索請求並解析谷歌產品的結果,我將非常感激。

回答

3

經過研究一些日子裏,我終於找到了一個解決方案:

谷歌給出瞭如何訪問保存在谷歌基地(hier)項目介紹.Surprisingly你不需要實現任何谷歌數據庫的API或任何可以訪問Google產品的內容,您都可以通過簡單的URL輕鬆查詢它們。

您可以通過URL http://www.google.com/base/feeds/snippets訪問Google Base和Google產品中的公共項目。您可以在此URL上附加特定查詢,例如:?bq =數字+相機,用於搜索數碼相機或?bq = 5030932067876,用於搜索實際的EAN代碼。

您找回保存該查詢結果的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>[email protected]</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> 

(...更多entrys來...)

您可以通過只是在做以下分析此文件:子類化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所應有的一切你剛解析的值。

希望它可以幫助別人!