How to use a domain instead of an IP in Java

0

I have an application that works very well in Local(área local) but when I use it outside of my Local Network, I can not because I need to use a domain. So I wanted to edit it and make it functional so that it works from outside LAN . Try using InetAddress and doing the following:

public LauncherActivity()
        throws UnknownHostException
        {
            this.giriAddress=InetAddress.getByName("MIDOMINIO");
            System.out.println("EJEMPLO"+giriAddress);

        }

The problem is that it only throws it to me when the exception occurs and I wanted my program to be able to get LA IP DE MI DOMINIO to be able to connect. Then I thought about removing the% share from% co but it returned the following error:

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

So my question is how could I make my program take the ip of my domain (it only does it when I throw the exception but it returns error) and I connect as if it will use my throws UnknownHostException . In my case the complete program would be:

package my.app.client;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import my.app.client.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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

        Intent Client, ClientAlt;
        // Button btnStart, btnStop;
        // EditText ipfield, portfield;
        private InetAddress giriAddress;

        public LauncherActivity()

        {
            this.giriAddress=InetAddress.getByName("www.google.com");
            System.out.println("EJEMPLO"+giriAddress);

        }   
        //private String hostIP = giriAddress.getHostAddress() ;
        private String myIp = "IP"; // Put your IP in these quotes.
        private int myPort = 12; // 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);
            }
    }
    
asked by Sergio Ramos 25.05.2017 в 22:54
source

2 answers

0

The application seems correct. The error Unhandled exception type UnknownHostException is due to an unhandled exception, try to put it within Try-catch :

try {
    this.giriAddress=InetAddress.getByName("www.google.com");
    System.out.println("EJEMPLO "+giriAddress);
} catch (UnknownHostException e) {
    // manejo de error
    e.printStackTrace();
}
    
answered by 26.05.2017 / 00:00
source
0

You should not remove the throws clause UnknownHostException, it is correct. If the code is throwing that Exception it is because it did not find the domain. Check your DNS in your network configuration. Test from a command console the command ping mydomain to see if you have access to the domain.

    
answered by 26.05.2017 в 00:32