views:

267

answers:

1

Hi -

I am trying to get the number of incoming links to a page through the Google Search API.

It is not working (just returning Null)

Here is the 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&amp;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>

Sorry for the ugly format.

I added a trace(event.target.data); and updated the code above.

This is the result -

[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}

So the data return from the query is correct, and the difficulty lies in accessing the "estimatedResultCount" near the end of the JSON data.

Any help would be greatly appreciated!

+1  A: 

I have not used JParser, I typically use corelib for JSON parsing. That said, you're simply accessing the value incorrectly. Try using this function.

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;
  }
}

As a side note, I would recommend using http://code.google.com/p/googleas3api/ to access google apis with flex.

h0tw1r3