问题描述
有这个网站:和我想让我的AS3代码去获取一些信息,并显示它的Flash。如果我不拥有网站,有可能吗?
例如: https://i.stack.imgur.com/7gonn.pngalt =在这里输入图片描述>
我想显示这些信息在我的AS3代码。这可能吗 ?我能怎么做 ?
Thx为您提供帮助,
编辑
Thx为VC的完整答案。我设法将信息粘贴到字符串中。
以下是我所做的:
var myString:String;
var request:URLRequest = new URLRequest(http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1& ;国家= NCD&安培; swellmap = 1&安培; _ = 1460963404274\" );
var loader:URLLoader = new URLLoader();
loader.load(request);
loader.addEventListener(Event.COMPLETE,weatherLoaded);
函数weatherLoaded(e:Event):void {
myString = e.target.data;
trace(myString); //输出是{tides:高:上午05:40(1.32米);低:下午12:10(0.57米);高:下午06:10(1.19米);,seatemp: 27& C,forecastdate:17h,rating:< img src ='http:\ / \ / www.swellmap.co.nz\ / style\ / img\\ \\ / weathericons \ / 1r.png'alt ='差条件'title ='差条件'\ />,rating_class:< span class ='徽章徽章 - 重要的alt = 'title ='Poor conditions'> 1< \ / span>,summary:< img class ='wx-summary'src ='http:\ / \ / www.swellmap.co。 \ />,title:Anse Vata,smaplink:http :\ / \ / www.swellmap.co.nz\ / surfing\ /新caledonia\ /昂斯-瓦塔, 瓦尔:{ hs_sw:{ 值: 0.4, 标题: 溶胀, 单元: M}, HS:{ 值: 0.6, 标题: 波, 单元: M}, wface :{value:0.8,title:Set face,unit:m},tp:{value:13,title单位 : S }, DPM :{value:S,title:Swell dir,unit:&#xb0;},windma:{value:E 12 :wind,unit:kts},gstma:{value:16,title:阵风,单位:kts}}}
}
现在,我不太明白怎么才能找到一些信息(比如膨胀的例子)。
是否有可能在AS3代码中显示我,我怎么能这样做? (在这个例子中,我们可以看到这个膨胀是0.4米@ 13秒)
例子是我想要做的:
function(searchTheSwell){
var swell_AnseVata;
swell_AnseVata = ....?
info_txt.text = swell_AnseVata;
你的浏览器,那么你会看到有一个PHP文件被访问来获取信息。它开始 http://www.swellmap.co.nz/ajr.php?r= ,你需要找到它为你说的。现在要查看内容,只需使用& callback = XYZ 删除网址部分,其中 XYZ 链接有..
1)
以下是如何获取位置数据的示例:
$ b 替换& s = ZZZZZ 与位置名称,所以如果我想 Anse Vata 它变成& s = Anse%20Vata 和 La Nera 变成& s = La%20Nera 。因此,在位置名称中对任何空格使用%20 。用例子& country = fra 替换& country = YYY 。
$ b要获取 Anse Vata 的JSON数据,请声明新的String变量,以便稍后保存JSON文本,然后在AS3中使用URLStream加载链接(在浏览器选项卡中打开以检查内容):
在URLStream的事件完成侦听器函数中,您可以使用(示例) myString = e.target.data; code>。然后在你的 myString 上使用JSON解析器,或者使用字符串函数(比如 indexOf 来进行单词搜索)手动执行。
3)如果您打开上面的 ,您将看到需要解析的JSON条目。
$ b
提取 @ 13 s 期间使用:
tp:{value:13,title:Period,unit:s}
src ='http:\ / \ / www.swellmap.co.nz\ / style\ / img\ / weathericons\ / suncloud.png' >
你必须清理图标链接,例如:
http://www.swellmap.co.nz/style/img/weathericons/suncloud.png
$编辑:
下面的代码从JSON字符串中提取预期的信息。只要将& s = 替换成任何其他位置(例如:& s = Ilot%20Tenia ), Swell,Wind和Icon的URL ...
var myURL:String =http://www.swellmap.co ?.NZ / ajr.php R =插件&安培; A =冲浪&安培; S =昂斯%20Vata&安培;国家= nz的&安培; swellmap = 1&安培;国家= NCD&安培; swellmap = 1&安培; _ = 1460963404274\" ;
var URL_Req:URLRequest = new URLRequest(myURL);
var URL_load:URLLoader = new URLLoader();
URL_load.addEventListener(Event.COMPLETE,completeHandler);
尝试{URL_load.load(URL_Req); }
catch(error:Error)
{trace(无法加载 - 请检查网址是否正确:+ error.message); }
var idx_start:int = 0; var idx_end:int = 0;
var str_Swell:String =;
var str_Swell_Period:String =;
var str_Swell_Dir:String =;
var str_Wind:String =; var url_Icon:String =;
函数completeHandler(e:Event):void
{
// var myData:Object = JSON.parse(e.target.data);
//对于每个(var s:* myData){trace(key:,s,value:,myData [s]); }
var myString:String = String(e.target.data);
// trace(myString:+ myString);
$ b $ //#获取Swell
idx_start = myString.indexOf(\hs_sw\:,0);
idx_end = myString.indexOf(,,idx_start + 1);
str_Swell = myString.substring(idx_start + 18,idx_end-1);
str_Swell = str_Swell +m;
// trace(Swell:+ str_Swell);
$ b $ //获取方向(用于膨胀)
idx_start = myString.indexOf(\dpm \:,0);
idx_end = myString.indexOf(,,idx_start + 1);
str_Swell_Dir = myString.substring(idx_start + 16,idx_end-1);
// trace(Swell dir:+ str_Swell_Dir);
$ b $ //获取时间段(对于Swell)
idx_start = myString.indexOf(\tp\:,0);
idx_end = myString.indexOf(,,idx_start + 1);
str_Swell_Period = myString.substring(idx_start + 15,idx_end-1);
str_Swell_Period =@+ str_Swell_Period +s;
// trace(Period:+ string_Period);
//#加入膨胀方向,尺寸和放大倍数句段成一句话
str_Swell = str_Swell_Dir ++ str_Swell + str_Swell_Period;
trace(Swell:+ str_Swell);
$ b $ //#获取Wind
idx_start = myString.indexOf(\windma \:,0);
idx_end = myString.indexOf(,,idx_start + 1);
str_Wind = myString.substring(idx_start + 19,idx_end-1);
str_Wind = str_Wind +kts;
trace(Wind:+ str_Wind);
##get Image URL
idx_start = myString.indexOf(\'wx-summary\',0);
idx_end = myString.indexOf(',idx_start + 18);
url_Icon = myString.substring(idx_start + 18,idx_end);
url_Icon = url_Icon.replace(/ \\ / g,);
trace(URL:+ url_Icon);
//#使用图片加载图片:new URLRequest(url_Icon);
}
There is this website : http://www.swellmap.co.nz/ and I'd like to make my AS3 code go fetch some infos and displays it flash.
Is it possible if I don't own the website ?
Exemple :
I want to display these infos in my AS3 code. Is this possible ? How can I do ?
Thx for your help,
EDIT
Thx to the full answer of VC.One I've managed to paste infos in a String.
Here's what I did :
var myString:String; var request:URLRequest = new URLRequest("http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274"); var loader:URLLoader = new URLLoader(); loader.load(request); loader.addEventListener(Event.COMPLETE,weatherLoaded); function weatherLoaded(e:Event):void{ myString = e.target.data; trace(myString); //output is {"tides":"High: 05:40 am (1.32 m); Low: 12:10 pm (0.57 m); High: 06:10 pm (1.19 m); ","seatemp":"27°C","forecastdate":"17h","rating":"<img src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/1r.png' alt='Poor conditions' title='Poor conditions' \/>","rating_class":"<span class='badge badge-important' alt='Poor conditions' title='Poor conditions'>1<\/span>","summary":"<img class='wx-summary' src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/suncloud.png' title='Sunny with some cloud' \/>","title":"Anse Vata","smaplink":"http:\/\/www.swellmap.co.nz\/surfing\/new-caledonia\/anse-vata","vars":{"hs_sw":{"value":"0.4","title":"Swell","unit":"m"},"hs":{"value":"0.6","title":"Wave","unit":"m"},"wface":{"value":"0.8","title":"Set face","unit":"m"},"tp":{"value":"13","title":"Period","unit":"s"},"dpm":{"value":"S","title":"Swell dir","unit":"°"},"windma":{"value":"E 12","title":"Wind","unit":"kts"},"gstma":{"value":"16","title":"Gusts","unit":"kts"}}} }
Now, I didn't quite understand how could I retrieve only some infos (like the swell for exemple). Is it possible to show me in AS3 code, how could I do that ? (in this exemple, we can see that the swell is "0.4 m @ 13 s")
exemple of what I'd like to do :
function(searchTheSwell){ var swell_AnseVata; swell_AnseVata =.... ? info_txt.text = swell_AnseVata; }
If you use the Developer Tools of your browser then you'll see that there's a PHP file being accessed to get the information. It starts http://www.swellmap.co.nz/ajr.php?r= and you need to find what it says for you. Now to view the contents just remove the part of the URL with &callback=XYZ where XYZ will be whatever the link has..
1)Here's an example of how to get data for a location :
Replace &s=ZZZZZ with name of location, so if I want Anse Vata it becomes &s=Anse%20Vata and La Nera becomes &s=La%20Nera. So use %20 for any spaces in location name. Replace &country=YYY with example &country=fra.
2)To get the JSON data for Anse Vata, declare your new String variable to later hold the JSON text and then just use URLStream in AS3 to load the link (open in browser tab to check contents) :
http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274
In the Event Complete listener function of URLStream you just use (example) myString = e.target.data;. Then use a JSON parser on your myString or do it manually yourself using String functions (like indexOf to word search).
3) If you opened the above link in an new tab you'll see the JSON entries you need to parse.
- Swell : "hs_sw":{"value":"0.4","title":"Swell","unit":"m"} and forextracting the @ 13 s period use :"tp":{"value":"13","title":"Period","unit":"s"}
- Wind : "windma":{"value":"E 12","title":"Wind","unit":"kts"}
- Icon : "summary":"<img class='wx-summary'src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/suncloud.png'
You'll have to clean up the icon links so it becomes for example : http://www.swellmap.co.nz/style/img/weathericons/suncloud.png
EDIT :
The code below extracts the expected information from the JSON string. Just replace &s= with any other location (eg: &s=Ilot%20Tenia) and it extracts the Swell, Wind and Icon URL entries...
var myURL : String = "http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274"; var URL_Req : URLRequest = new URLRequest( myURL ); var URL_load:URLLoader = new URLLoader(); URL_load.addEventListener(Event.COMPLETE, completeHandler); try { URL_load.load( URL_Req ); } catch (error:Error) { trace("Could not load - Please check URL is correct : " + error.message); } var idx_start : int = 0; var idx_end : int = 0; var str_Swell : String = ""; var str_Swell_Period : String = ""; var str_Swell_Dir : String = ""; var str_Wind : String = ""; var url_Icon : String = ""; function completeHandler(e : Event):void { //var myData : Object = JSON.parse(e.target.data); //for each (var s:* in myData) { trace("key:",s,"value:",myData[s]); } var myString : String = String(e.target.data); //trace ( "myString : " + myString); //# Get Swell idx_start = myString.indexOf("\"hs_sw\":" , 0 ); idx_end = myString.indexOf("," , idx_start + 1 ); str_Swell = myString.substring(idx_start + 18, idx_end-1); str_Swell = str_Swell + " m"; //trace ("Swell : " + str_Swell ); //# Get Direction (for Swell) idx_start = myString.indexOf("\"dpm\":" , 0 ); idx_end = myString.indexOf("," , idx_start + 1 ); str_Swell_Dir = myString.substring(idx_start + 16, idx_end-1); //trace ("Swell dir : " + str_Swell_Dir ); //# Get time Period (for Swell) idx_start = myString.indexOf("\"tp\":" , 0 ); idx_end = myString.indexOf("," , idx_start + 1 ); str_Swell_Period = myString.substring(idx_start + 15, idx_end-1); str_Swell_Period = " @ " + str_Swell_Period + " s"; //trace ("Period : " + string_Period ); //# Join Swell Direction, Size & Period entries into one sentence str_Swell = str_Swell_Dir + " " + str_Swell + str_Swell_Period; trace ("Swell : " + str_Swell ); //# Get Wind idx_start = myString.indexOf("\"windma\":" , 0 ); idx_end = myString.indexOf("," , idx_start + 1 ); str_Wind = myString.substring(idx_start + 19, idx_end-1); str_Wind = str_Wind + " kts"; trace ("Wind : " + str_Wind ); //# get Image URL idx_start = myString.indexOf("\'wx-summary\'" , 0 ); idx_end = myString.indexOf("'" , idx_start + 18 ); url_Icon = myString.substring(idx_start + 18, idx_end); url_Icon = url_Icon.replace(/\\/g, ""); trace ("URL : " + url_Icon ); //# load image using : new URLRequest (url_Icon); }
这篇关于让我的AS3代码去获取我不拥有的网站上的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!