这是调用webapis后实现的一个搜索功能,还不错。必须在项目中加一个web API类库。
下载地址
源码
- <?xml version="1.0" encoding="utf-8"?>
- <s:WindowedApplication xmlns:fx=""
- xmlns:s="library://ns.adobe.com/flex/spark"
- xmlns:mx="library://ns.adobe.com/flex/mx" viewSourceURL="srcview/index.html">
- <fx:Declarations>
-
- </fx:Declarations>
-
- <fx:Script>
- <![CDATA[
- import com.adobe.webapis.events.*;
- import com.adobe.webapis.*;
- import com.adobe.serialization.json.*;
- import mx.controls.Alert;
- import mx.rpc.events.ResultEvent;
- import mx.rpc.http.HTTPService;
-
- [Bindable]
- private var dgResults:Array; // source for datagrid
-
- private function search():void
- {
- var service:HTTPService = new HTTPService();
- service.url = '';
- service.request.v = '1.0';
- service.request.q = searchStr.text;
- service.resultFormat = 'text';
- service.addEventListener(ResultEvent.RESULT, onServerResponse);
- service.send();
- }
- private function onServerResponse(event:ResultEvent):void {
- try {
- var json:Object = JSON.decode(event.result as String);
- if (json.responseData.results is Array) {
- dg.dataProvider = json.responseData.results;
- }
- }
- catch (error:Error) {
- Alert.show("Error on search: " + error.message);
- }
- }
- private function goToURL():void
- {
- navigateToURL(new URLRequest(dg.selectedItem.url))
- }
- ]]>
- </fx:Script>
-
- <mx:HBox>
- <mx:Text text="Search Google:" color="#FFFFFF"/>
- <mx:TextInput id="searchStr" enter="search()" width="200"/>
- <mx:Button id="btnSearch" click="search()" label="搜索"/>
- </mx:HBox>
-
- <mx:DataGrid id="dg" dataProvider="{dgResults}" width="90%" height="113" minColumnWidth="200"
- variableRowHeight="false" doubleClickEnabled="true" doubleClick="goToURL()" x="16" y="22">
- <mx:columns>
- <mx:DataGridColumn dataField="url" headerText="URL" width="200"/>
- <mx:DataGridColumn dataField="titleNoFormatting" headerText="Title" wordWrap="true" width="300"/>
- </mx:columns>
- </mx:DataGrid>
-
- </s:WindowedApplication>
这是一个air程序,效果如图:
阅读(2581) | 评论(0) | 转发(0) |