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.
Cheers Guys!!!!!
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;
ListhostList = 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");
ArrayAdapteradapter
= 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!!!!!
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :