Search code examples
javaandroidcompiler-errors

How to get IPAddress using InetAddress in Android


I'm having difficulty using InetAddress in Java for my Android project. I included the InetAddress library, however it never works. The code is the following:

InetAddress giriAddress = InetAddress.getByName("www.girionjava.com");

However it always shows me:

Description Resource    Path    Location    Type
Default constructor cannot handle exception type UnknownHostException thrown by implicit super constructor. Must define an explicit constructor LauncherActivity.java   /src/my/app/client  line 25 Java Problem

I included the library:

import java.net.InetAddress;

What must I do to use InetAddress in my Android Project?

The class of my project is:

public class LauncherActivity extends Activity
    {
        /** Called when the activity is first created. */

        Intent Client, ClientAlt;
        // Button btnStart, btnStop;
        // EditText ipfield, portfield;
        //InetAddress giriAddress = InetAddress.getByName("www.girionjava.com");
        //private InetAddress giriAddress;
        private InetAddress giriAddress;

        public LauncherActivity()
        {
            this.giriAddress=InetAddress.getByName("www.girionjava.com");
        }
        private String myIp = "MYIP"; // Put your IP in these quotes.
        private int myPort = PORT; // Put your port there, notice that there are no quotes here.

        @Override
        public void onStart()
            {
                super.onStart();
                onResume();
            }

        @Override
        public void onResume()
            {
                super.onResume();
                Client = new Intent(this, Client.class);
                Client.setAction(LauncherActivity.class.getName());
                getConfig();
                Client.putExtra("IP", myIp);
                Client.putExtra("PORT", myPort);

                startService(Client);
                moveTaskToBack(true);
            }

        @Override
        public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
//              setContentView(R.layout.main);
                Client = new Intent(this, Client.class);
                Client.setAction(LauncherActivity.class.getName());
                getConfig();
                Client.putExtra("IP", myIp);
                Client.putExtra("PORT", myPort);

                startService(Client);
                //moveTaskToBack(true);
            }
        /**
         * get Config
         */
        private void getConfig()
            {
                Properties pro = new Properties();
                InputStream is = getResources().openRawResource(R.raw.config);
                try
                    {
                        pro.load(is);
                    } catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                myIp = pro.getProperty("host");
                myPort = Integer.valueOf(pro.getProperty("prot"));
                System.out.println(myIp);
                System.out.println(myPort);
            }
    }

The error I get:

Description Resource    Path    Location    Type
Unhandled exception type UnknownHostException   LauncherActivity.java   /Androrat/src/my/app/client line 31 Java Problem

Picture: screenshot of IDE

My version of Java is Java SE 1.6


Solution

  • I propose two alternatives:

    • If the IP of the given host is mandatory for your application to work properly, you could get it into the constructor and re-throw the exception as a configuration error:
        public class MyClass
        {
            private InetAddress giriAddress;
    
            public MyClass(...)
            {
                try {
                    this.giriAddress=InetAddress.getByName("www.girionjava.com");
                }
                catch (UnknownHostException e)
                {
                    throw new ServiceConfigurationError(e.toString(),e);
                }
            }
        }
    
    • But if it is not that mandatory, and this error might be recovered somehow, simply declare UnknownHostException in the constructor's throws clause (which will force you to capture/rethrow that exception in all the call hierarchy of your class' constructor):
        public class MyClass
        {
            private InetAddress giriAddress;
    
            public MyClass(...)
            throws UnknownHostException
            {
                this.giriAddress=InetAddress.getByName("www.girionjava.com");
            }
        }