Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2228453
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: 系统运维

2011-07-01 10:24:55

这是调用webapis后实现的一个搜索功能,还不错。必须在项目中加一个web API类库。
下载地址
 
源码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:WindowedApplication xmlns:fx=""
  3.                      xmlns:s="library://ns.adobe.com/flex/spark"
  4.                      xmlns:mx="library://ns.adobe.com/flex/mx" viewSourceURL="srcview/index.html">
  5.     <fx:Declarations>
  6.         
  7.     </fx:Declarations>
  8.     
  9.      <fx:Script>
  10.          <![CDATA[
  11.              import com.adobe.webapis.events.*;
  12.              import com.adobe.webapis.*;
  13.              import com.adobe.serialization.json.*;
  14.              import mx.controls.Alert;
  15.              import mx.rpc.events.ResultEvent;
  16.              import mx.rpc.http.HTTPService;

  17.             
  18.              [Bindable]
  19.              private var dgResults:Array; // source for datagrid
  20.            
  21.              private function search():void
  22.              {
  23.                  var service:HTTPService = new HTTPService();
  24.                  service.url = '';
  25.                  service.request.v = '1.0';
  26.                  service.request.q = searchStr.text;
  27.                  service.resultFormat = 'text';
  28.                  service.addEventListener(ResultEvent.RESULT, onServerResponse);
  29.                  service.send();
  30.              }
  31.              private function onServerResponse(event:ResultEvent):void {
  32.                  try {
  33.                      var json:Object = JSON.decode(event.result as String);
  34.                      if (json.responseData.results is Array) {
  35.                          dg.dataProvider = json.responseData.results;
  36.                      }
  37.                  }
  38.                  catch (error:Error) {
  39.                      Alert.show("Error on search: " + error.message);
  40.                  }
  41.              }
  42.              private function goToURL():void
  43.              {
  44.                  navigateToURL(new URLRequest(dg.selectedItem.url))
  45.              }
  46.          ]]>
  47.      </fx:Script>
  48.     
  49.     <mx:HBox>
  50.     <mx:Text text="Search Google:" color="#FFFFFF"/>
  51.     <mx:TextInput id="searchStr" enter="search()" width="200"/>
  52.     <mx:Button id="btnSearch" click="search()" label="搜索"/>
  53.     </mx:HBox>
  54.     
  55.     <mx:DataGrid id="dg" dataProvider="{dgResults}" width="90%" height="113" minColumnWidth="200"
  56.                  variableRowHeight="false" doubleClickEnabled="true" doubleClick="goToURL()" x="16" y="22">
  57.         <mx:columns>
  58.             <mx:DataGridColumn dataField="url" headerText="URL" width="200"/>
  59.             <mx:DataGridColumn dataField="titleNoFormatting" headerText="Title" wordWrap="true" width="300"/>
  60.         </mx:columns>
  61.     </mx:DataGrid>
  62.     
  63. </s:WindowedApplication>
这是一个air程序,效果如图:
阅读(2581) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~