USTREAM のチャンネルIDを求めるのに前は Java でHTTP通信するのを作成した。
今度は、AIRで作ってみた。
前はチャンネルID以外の情報も取得するURL、
http://api.ustream.tv/xml/channel/channel名/getInfo?key=API-Key
だったが今回はチャンネルIDだけを求めるURL、
http://api.ustream.tv/xml/channel/channel名/getValueOf/id?key=API-Key
で求める。
http リクエストパラメータの書き方もAIRが醜いと感じた。
AS ソース、、、
package org.ustch
{
import flash.events.MouseEvent;
import flash.net.URLRequestMethod;
import mx.controls.Alert;
import mx.core.IMXMLObject;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.mxml.HTTPService;
import spark.components.Label;
import spark.components.TextInput;
public class UstchSerche implements IMXMLObject{
private var chid:Label;
private var chname:TextInput;
public function UstchSerche(){ }
public function initialized(document:Object, id:String):void{
}
public function init(ch_id:Label,chname:TextInput):void{
this.chid = ch_id;
this.chname = chname;
}
public function search_clickHandler(event:MouseEvent):void{
trace(chname.text);
if (chname.text != null){
var http:HTTPService = new HTTPService();
http.method = URLRequestMethod.GET;
http.url = "http://api.ustream.tv/xml/channel/"+chname.text+"/getValueOf/id";
http.request = {"key": "developer.ustream.tvで取得したAPI-Key"};
http.addEventListener(ResultEvent.RESULT,resultHandler);
http.addEventListener(FaultEvent.FAULT,faultHandler);
http.send();
}
}
private function resultHandler(e:ResultEvent):void{
var xml:Object = e.result.xml;
if (xml != null){
chid.text = xml.results;
}
}
private function faultHandler(e:FaultEvent):void{
Alert.show(e.message);
}
}
}
mxml ソースは、、、、
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:logic="org.ustch.*"
initialize="{ust.init(chid,chname)}">
<fx:Declarations>
<logic:UstchSerche id="ust"/>
</fx:Declarations>
<s:TextInput id="chname" x="133" y="49"/>
<s:Label id="chid" x="133" y="91" width="128" height="23" fontSize="16"/>
<s:Button x="299" y="49" label="探索" click="ust.search_clickHandler(event)"/>
</s:WindowedApplication>