我在IOS和Android上都有一个adobe air应用程序。我在互联网上搜索过,看看如何检查设备是否连接到工作连接。最合乎逻辑的方式如下:
使用以下内容创建ConnectionChecker类:
package
{
import flash.events.*;
import flash.net.*;
[Event(name="error", type="flash.events.Event")]
[Event(name="success", type="flash.events.Event")]
public class ConnectionChecker extends EventDispatcher
{
public static const EVENT_SUCCESS:String = "success";
public static const EVENT_ERROR:String = "error";
// Though google.com might be an idea, it is generally a better practice
// to use a url with known content, such as http://foo.com/bar/mytext.txt
// By doing so, known content can also be verified.
// This would make the checking more reliable as the wireless hotspot sign-in
// page would negatively intefere the result.
private var _urlToCheck:String = "http://www.google.com";
// empty string so it would always be postive
private var _contentToCheck:String = "";
public function ConnectionChecker()
{
super();
}
public function check():void
{
var urlRequest:URLRequest = new URLRequest(_urlToCheck);
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, loader_complete);
loader.addEventListener(IOErrorEvent.IO_ERROR, loader_error);
try
{
loader.load(urlRequest);
}
catch ( e:Error )
{
dispatchErrorEvent();
}
}
private function loader_complete(event:Event):void
{
var loader:URLLoader = URLLoader( event.target );
var textReceived:String = loader.data as String;
if ( textReceived )
{
if ( textReceived.indexOf( _contentToCheck ) )
{
dispatchSuccessEvent();
}
else
{
dispatchErrorEvent();
}
}
else
{
dispatchErrorEvent();
}
}
private function loader_error(event:IOErrorEvent):void
{
dispatchErrorEvent();
}
private function dispatchSuccessEvent():void
{
dispatchEvent( new Event( EVENT_SUCCESS ) );
}
private function dispatchErrorEvent():void
{
dispatchEvent( new Event( EVENT_ERROR ) );
}
}
}
之后,每当我需要实际测试是否存在工作连接时,我都会执行以下操作:
var checker:ConnectionChecker = new ConnectionChecker();
checker.addEventListener(ConnectionChecker.EVENT_SUCCESS, checker_success);
checker.addEventListener(ConnectionChecker.EVENT_ERROR, checker_error);
checker.check();
适当的功能是:
private function checker_success(event:Event):void
{
trace("U have cnx");
continueTheGame.visible = true;
// There is internet connection
}
private function checker_error(event:Event):void
{
trace("U dn't have cnx");
continueTheGame.visible = false;
// There is no internet connection
}
以上所有答案均基于我在SO https://stackoverflow.com/a/13569775/5128351
上找到的此链接现在的问题是,无论我是否有连接,我都会得到没有连接可用的结果。我错过了什么吗?
编辑:
我尝试将以下内容添加到xml文件中,但我仍然得到相同的结果:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>google.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
答案 0 :(得分:1)
关于_contentToCheck
字符串:
//空字符串,所以它总是正的
private var _contentToCheck:String = "";
我建议您检查加载的HTML源代码中的实际&#34; target&#34; 文本。这样,如果它存在,您可以确定至少Google已加载O.K(设备已连接)。
// check string of HTML to confirm web load was successful
private var _contentToCheck:String = "<!doctype html>"; //beginning of Google's HTML text
关于Check()
功能
如果您在loader_complete
事件中使用如下逻辑,会发生什么?
private function loader_complete(event:Event):void
{
var loader:URLLoader = URLLoader( event.target );
var textReceived:String = loader.data as String;
if ( textReceived.length > 0 )
{
//index will be -1 if search text is not found (use IS NOT EQUAL to check this)
if ( textReceived.indexOf( _contentToCheck ) != -1 )
{
dispatchSuccessEvent();
}
else
{
dispatchErrorEvent();
}
}
else
{
dispatchErrorEvent();
}
}
代码未经过编译器测试,但逻辑应该适合您自己的测试。希望能帮助到你。
以下是我测试的两个代码(正确检测开/关互联网连接)
重新启用函数loader_complete
中的那些跟踪以仔细检查任何内容。
(1) FLA与此Connect_test_01.as
(主要AS类)相关联
package
{
import flash.display.MovieClip;
import flash.events.*;
import ConnectionChecker;
public class Connect_test_01 extends MovieClip
{
public var checker:ConnectionChecker;
public function Connect_test_01()
{
checker = new ConnectionChecker();
checker.addEventListener(ConnectionChecker.EVENT_SUCCESS, checker_success);
checker.addEventListener(ConnectionChecker.EVENT_ERROR, checker_error);
checker.check();
}
private function checker_success(event:Event):void
{
//# There is internet connection
trace("-- YES - Net found.. You are connected");
//continueTheGame.visible = true;
}
private function checker_error(event:Event):void
{
//# There is no internet connection
trace("-- NO - Net not found... No connection");
//continueTheGame.visible = false;
}
} //end Class
} //end Package
(2)这是上述代码导入中使用的ConnectionChecker.as
。
package
{
import flash.events.*;
import flash.net.*;
[Event(name="error", type="flash.events.Event")]
[Event(name="success", type="flash.events.Event")]
public class ConnectionChecker extends EventDispatcher
{
public var urlRequest:URLRequest;
public var loaderA:URLLoader;
public var loaderB:URLLoader;
public var textReceived:String;
public static const EVENT_SUCCESS:String = "success";
public static const EVENT_ERROR:String = "error";
// URL to connect for testing
private var _urlToCheck:String = "https://www.google.com";
// check string within HTML content to confirm web load was successful
private var _contentToCheck:String = "<!doctype html>"; //beginning of Google's HTML text
public function ConnectionChecker()
{ super(); }
public function check():void
{
urlRequest = new URLRequest(_urlToCheck);
loaderA = new URLLoader();
loaderA.dataFormat = URLLoaderDataFormat.TEXT;
loaderA.addEventListener(Event.COMPLETE, loader_complete);
loaderA.addEventListener(IOErrorEvent.IO_ERROR, loader_error);
try { loaderA.load(urlRequest); }
catch ( e:Error ) { dispatchErrorEvent(); }
}
private function loader_complete(event:Event):void
{
loaderB = URLLoader( event.target );
textReceived = String(loaderB.data); // as String;
//# check contents of load (should be HTML source-code)
//trace ("STRING CHECK : " + "\n" + textReceived + "\n");
//# minus-1 means Not Found. Should be Zero since Search Text is beginning
//trace("Index of String : " + textReceived.indexOf( _contentToCheck ) + "\n" );
if ( textReceived.length > 0 )
{
//index will be -1 if search text is not found (use IS NOT EQUAL to check this)
if ( textReceived.indexOf( _contentToCheck ) != -1 )
{ dispatchSuccessEvent(); }
else { dispatchErrorEvent(); }
}
else
{ dispatchErrorEvent(); }
}
private function loader_error(event:IOErrorEvent):void
{ dispatchErrorEvent(); }
private function dispatchSuccessEvent():void
{ dispatchEvent( new Event( EVENT_SUCCESS ) ); }
private function dispatchErrorEvent():void
{ dispatchEvent( new Event( EVENT_ERROR ) ); }
}
}
答案 1 :(得分:0)
Adobe创建了一个URLMonitor类来处理这个问题。
http://help.adobe.com/en_US/air/reference/html/air/net/URLMonitor.html
答案 2 :(得分:0)
您是否已将现在需要的NSTransportSecurity XML添加到iOS应用描述符中?您需要指定要与之通信的所有域。
这是在iPhone节点内部。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>google.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>another domain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>