Error: Definition flash.net:NetworkInfo could not be found while using import flash.net.NetworkInfo

643 views Asked by At

I am trying to build a swf file using actionscript code. I have downloaded Flex SDK. Now I am trying to compile .as file. It works fine and compiles into .swf file using the following command from the bin folder of Flex Home. .

Command: /mxmlc /home/anshul/Downloads/HelloWorld/src/Main.as

FileName: Main.as

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;   

    public class Main extends Sprite
    {

        public function Main():void
        {
            if (stage) init();
        }

        private function init(e:Event = null):void
        {
            var tf:TextField = new TextField();
            tf.text = "Hello, world!, Lets see working or not";
            addChild( tf );
        }

    }

}

This is a basic example and works fine. But if I add a import,

import flash.net.NetworkInfo;

then it throws the following error Error: Definition flash.net:NetworkInfo could not be found.

I have gone through various tutorials but can't seem to make it work. So do I need to include any library while running the command?

2

There are 2 answers

0
Brian On

As @akmozo said, flash.net.NetworkInfo is an AIR class, so you need to include the AIR config in your build command:

/mxmlc ./Main.as -load-config+=$FLEX_HOME/frameworks/air-config.xml
2
akmozo On

Before speaking about your code, you should know that flash.net.NetworkInfo is only available for Air 2 and up. And as Adobe said about it : "... This feature is supported on all desktop operating systems and AIR for TV devices, but is not supported on all mobile devices. You can test for support at run time using the NetworkInfo.isSupported property. See AIR Profile Support for more information regarding API support across multiple profiles. ...", it is not supported on all mobile devices, that's why It's better to use NetworkInfo.isSupported to verify if it's supported or not.

For how to use it, take this example from Adobe.com which I have implemented in this code :

<?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"
                       width="620" height="573" creationComplete="init(event)">
    <fx:Script>
        <![CDATA[           

            import flash.net.NetworkInfo;           
            import mx.events.FlexEvent; 

            protected function init(event:FlexEvent):void
            {
                // enable the button only if NetworkInfo is supported
                btn.enabled = NetworkInfo.isSupported;
            }           
            protected function btn_clickHandler(event:MouseEvent):void
            {
                // get device network interfaces
                var output:String = '';
                var results:Vector.<NetworkInterface> = NetworkInfo.networkInfo.findInterfaces(); 
                for (var i:int=0; i<results.length; i++) { 
                    output = output + "Name: " + results[i].name + "\n" 
                        + "DisplayName: " + results[i].displayName + "\n" 
                        + "MTU: " + results[i].mtu + "\n" 
                        + "HardwareAddr: " + results[i].hardwareAddress + "\n" 
                        + "Active: " + results[i].active + "\n"; 
                    for (var j:int=0; j<results[i].addresses.length; j++) { 
                        output = output + "Addr: " + results[i].addresses[j].address + "\n" 
                            + "Broadcast: " + results[i].addresses[j].broadcast + "\n" 
                            + "PrefixLength: " + results[i].addresses[j].prefixLength + "\n" 
                            + "IPVersion: " + results[i].addresses[j].ipVersion + "\n"; 
                    } 
                    output = output + "\n"; 
                } 
                txt.text = output;
            }

        ]]>
    </fx:Script>
    <s:Button id="btn" x="10" y="10" width="192" height="33" label="Get Network Interfaces"
              click="btn_clickHandler(event)"/>
    <s:TextArea id="txt" x="8" y="45" width="602" height="509"/>
</s:WindowedApplication>