使用Flex访问Google Search API很困难

时间:2010-01-25 04:35:25

标签: flex adobe flex3

我正在尝试通过Google Search API获取指向某个网页的传入链接的数量。

它不起作用(只返回Null)

这是代码

<?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: wayne.ivor@googlemail.com , dappa2000@blueyonder.co.uk
  //

  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>

抱歉丑陋的格式。

我添加了一条跟踪(event.target.data);并更新了上面的代码。

这是结果 -

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

因此从查询返回的数据是正确的,难点在于访问JSON数据末尾附近的“estimatedResultCount”。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

我没有使用过JParser,我通常使用corelib进行JSON解析。也就是说,你只是错误地访问了这个值。尝试使用此功能。

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

作为附注,我建议使用http://code.google.com/p/googleas3api/来访问带有flex的google apis。