Sunday, February 24, 2013

Gets IP addresses of a given host - InetAddress.getAllByName()

java.net.InetAddress is a class of Internet Protocol (IP) address. This can be either an IPv4 address or an IPv6 address, and in practice you'll have an instance of either Inet4Address or Inet6Address (this class cannot be instantiated directly). Most code does not need to distinguish between the two families, and should use InetAddress.

The method getAllByName() gets all IP addresses associated with the given host identified by name or literal IP address. The IP address is resolved by the configured name service. If the host name is empty or null an UnknownHostException is thrown. If the host name is a literal IP address string an array with the corresponding single InetAddress is returned.

This example list all IP addresses associated with the user enter host name.

Note:

"android.permission.INTERNET" is needed.

For Applications targeting the Honeycomb SDK or higher, cannot attempts to perform a networking operation on its main thread. Otherwise, NetworkOnMainThreadException will be thrown. So the network operation is moved into AsyncTask.

I haven't IPv6 connection currently, I don't know if it will show Inet6Address with IPv6 connection. If you have, please let me know.

Java Code:

  
package com.AndroidInet;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class AndroidInetActivity extends Activity {

EditText hostinput;
TextView info;
Button btnTest;
ListView resultList;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hostinput = (EditText)findViewById(R.id.testhost);
info = (TextView)findViewById(R.id.info);
resultList = (ListView)findViewById(R.id.result);
btnTest = (Button)findViewById(R.id.test);
btnTest.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View view) {
info.setText("Wait...");
new Task().execute();

}});
}



private class Task extends AsyncTask{
Boolean error = false;
String error_info = "";
InetAddress[] inetAddress = null;

List hostList = new ArrayList();

@Override
protected Void doInBackground(Void... arg0) {
doTest();
return null;
}

@Override
protected void onPostExecute(Void result) {
if(error){
info.setText("Error: \n" + error_info);
}else{
info.setText("Finished");

ArrayAdapter adapter
= new ArrayAdapter(
AndroidInetActivity.this,
android.R.layout.simple_list_item_1,
hostList);

resultList.setAdapter(adapter);
}

super.onPostExecute(result);
}

private void doTest(){

String host = hostinput.getText().toString();

try {
inetAddress = InetAddress.getAllByName(host);

for(int i = 0; i < inetAddress.length; i++){

hostList.add(inetAddress[i].getClass() + " -\n"
+ inetAddress[i].getHostName() + "\n"
+ inetAddress[i].getHostAddress());
}

} catch (UnknownHostException e) {

e.printStackTrace();

error = true;
error_info = e.toString();
}

}
}
}

XML Code:

 







Cheers Guys!!!!!

Rocky

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Tipss.