An InetAddress may have a hostname (accessible via getHostName), but may not, depending on how the InetAddress was created.
In this exercise, we are going to get all IP addresses associated with a host, by calling the function getAllByName().
package com.exercise.AndroidIP;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidIP extends Activity {
EditText inputHostName;
Button btnCheck;
TextView textInetAddress;
final static String DEFAULT_HOST_NAME = "www.google.com";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputHostName = (EditText)findViewById(R.id.hostname);
btnCheck = (Button)findViewById(R.id.check);
textInetAddress = (TextView)findViewById(R.id.InetAddress);
inputHostName.setText(DEFAULT_HOST_NAME);
btnCheck.setOnClickListener(btnCheckOnClickListener);
}
Button.OnClickListener btnCheckOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String hostName = inputHostName.getText().toString();
try {
InetAddress[] hostInetAddress
= InetAddress.getAllByName(hostName);
String all = "";
for(int i = 0; i < hostInetAddress.length; i++){
all = all + String.valueOf(i) + " : "
+ hostInetAddress[i].toString() + "\n";
}
textInetAddress.setText(all);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(AndroidIP.this, e.toString(),
Toast.LENGTH_LONG).show();
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter Host Name"
/>
<EditText
android:id="@+id/hostname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Check "
/>
<TextView
android:id="@+id/InetAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Grant permission of "android.permission.INTERNET"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidIP"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidIP"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Download the files.
No comments:
Post a Comment