2010-01-25 6 views
0

J'essaie d'obtenir le nombre de liens entrants vers une page via l'API de recherche Google.Difficulté d'accès à l'API de recherche Google avec Flex

Il ne fonctionne pas (juste retour nul)

Voici le code

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();" width="320" height="480" backgroundGradientColors="115115" backgroundGradientAlphas=".2" backgroundAlpha=".2" dropShadowEnabled="false"> 

<mx:Script> 
<![CDATA[ 
    // 
    // Author: Wayne IV Mike 
    // Project: JSwoof - The Flex JSON library. 
    // Description: Formated JSON loaded from txt file. 
    // Date: 31st March 2009. 
    // Contact: [email protected] , [email protected] 
    // 

    import json.*; 
    import mx.controls.Alert; 

    public function loadFile4(urlLink:String):void 
    { 
    var request:URLRequest = new URLRequest(urlLink); 
    var urlLoad:URLLoader = new URLLoader(); 

    urlLoad.addEventListener(Event.COMPLETE, fileLoaded4); 
    urlLoad.load(request); 
    } 

    private function fileLoaded4(event:Event):void 
    { 
    var jObj:Object = JParser.decode(event.target.data); //Decode JSON from text file here. 

    var jStr:String = JParser.encode(jObj); 

    if(jStr != null && jStr != "") 
    { 
    var LinkTemp:String = jObj.estimatedResultCount; 

    txtLinks.text = "Google Links " + LinkTemp; 
    trace(event.target.data); 


    } 


    } 

    /********************************************************************/ 

    private function LinkLookup():void 
    { 


    loadFile4("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=link:twitter.com/" + NameSearch.text); 



} 


]]> 
</mx:Script> 


<mx:TextInput x="17" y="86" id="NameSearch" text="cnnbrk" width="229" height="30" fontSize="16" fontWeight="bold" cornerRadius="10" shadowDirection="center" shadowDistance="5"/> 
<mx:Button x="253" y="85" label="Find" id="GoSearch" click="LinkLookup()" height="31"/> 
<mx:Label text="Links" id="txtLinks" width="233" textAlign="left" color="#FFFFFF" fontSize="14" height="21"/> 

</mx:Application> 

Désolé pour le format laid.

J'ai ajouté une trace (event.target.data); et mis à jour le code ci-dessus.

Ceci est le résultat -

[SWF] C:/Documents and Settings/Robert/My Documents/Flex Builder 3/.metadata/.plugins/com.adobe.flash.profiler/ProfilerAgent.swf - 17,508 bytes after decompression 
[SWF] C:\Documents and Settings\Robert\My Documents\Flex Builder 3\Formated\bin-debug\Formated.swf - 781,950 bytes after decompression 
[Unload SWF] C:/Documents and Settings/Robert/My Documents/Flex Builder 3/.metadata/.plugins/com.adobe.flash.profiler/ProfilerAgent.swf 
{"responseData": {"results":[{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://twitter.com/britishredneck","url":"http://twitter.com/britishredneck","visibleUrl":"twitter.com","cacheUrl":"http://www.google.com/search?q\u003dcache:4pQXnMQCZA4J:twitter.com","title":"Martyn Jones (BritishRedneck) on Twitter","titleNoFormatting":"Martyn Jones (BritishRedneck) on Twitter","content":"Finally found a free and simple way to expand my reach on Twitter. A nice 20 second process. http://tpq.me/5gbrg #twpq 3:13 PM Jul 18th, 2009 from API \u003cb\u003e...\u003c/b\u003e"},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://twitter.com/dshlian/favorites","url":"http://twitter.com/dshlian/favorites","visibleUrl":"twitter.com","cacheUrl":"http://www.google.com/search?q\u003dcache:79qm5Pz7O5QJ:twitter.com","title":"Twitter","titleNoFormatting":"Twitter","content":"Twitter is without a doubt the best way to share and discover what is happening right now."},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://twitter.com/rosannepeterson","url":"http://twitter.com/rosannepeterson","visibleUrl":"twitter.com","cacheUrl":"http://www.google.com/search?q\u003dcache:q11IcnW9l30J:twitter.com","title":"Rosanne Peterson (rosannepeterson) on Twitter","titleNoFormatting":"Rosanne Peterson (rosannepeterson) on Twitter","content":"Tx.All is well. Looking forward to the holday. Perhaps after will be time for certification! 8:14 AM Dec 23rd, 2009 from txt; I am also reading \u0026quot;How I \u003cb\u003e...\u003c/b\u003e"},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://twitter.com/MRSalesTraining","url":"http://twitter.com/MRSalesTraining","visibleUrl":"twitter.com","cacheUrl":"http://www.google.com/search?q\u003dcache:uBNGhud0vfEJ:twitter.com","title":"Medrep (MRSalesTraining) on Twitter","titleNoFormatting":"Medrep (MRSalesTraining) on Twitter","content":"Working away on Cardiovascular Medicine Module - heavy stuff for a Sunday evening!! 11:09 AM Nov 8th, 2009 from web; Today\u0026#39;s Student is tomorrow\u0026#39;s Medical \u003cb\u003e...\u003c/b\u003e"}],"cursor":{"pages":[{"start":"0","label":1},{"start":"4","label":2},{"start":"8","label":3},{"start":"12","label":4},{"start":"16","label":5},{"start":"20","label":6},{"start":"24","label":7},{"start":"28","label":8}],"estimatedResultCount":"64","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dlink%3Atwitter.com%2Fgenericmedlist"}}, "responseDetails": null, "responseStatus": 200} 

Ainsi, le retour de données de la requête est correcte, et la difficulté réside dans l'accès à la « estimatedResultCount » à la fin des données JSON.

Toute aide serait grandement appréciée!

+0

Que fait 'trace (event.target.data);'? – Amarghosh

Répondre

1

Je n'ai pas utilisé JParser, j'utilise généralement corelib pour l'analyse JSON. Cela dit, vous accédez simplement incorrectement à la valeur. Essayez d'utiliser cette fonction.

private function fileLoaded4(event:Event):void 
{ 
    var jObj:Object = JSON.decode(event.target.data).responseData; 

    if (jObj.results) { 
    var LinkTemp:String = jObj.cursor.estimatedResultCount; 

    txtLinks.text = "Google Links " + LinkTemp; 
    } 
} 

Comme une note de côté, je vous recommande d'utiliser http://code.google.com/p/googleas3api/ pour accéder à Google APIs avec flex.