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.
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<Void, Void, Void>{ Boolean error = false; String error_info = ""; InetAddress[] inetAddress = null; List<String> hostList = new ArrayList<String>(); @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<String> adapter = new ArrayAdapter<String>( 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 version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/testhost" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="www.worldipv6launch.org" /> <Button android:id="@+id/test" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Test" /> <TextView android:id="@+id/info" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
No comments:
Post a Comment