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

Thảo luận trong 'Điện thoại, viễn thông' bắt đầu bởi linhngok, 23/9/12.

  1. linhngok New Member

    linhngok

    Tham gia ngày:
    16/8/12
    Bài viết:
    23
    Đã được thích:
    7
    Điểm thành tích:
    0
    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.

    [​IMG]


    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.


    PHP:
    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<VoidVoidVoid>{
         
    Boolean error false;
         
    String error_info "";
         
    InetAddress[] inetAddress null;
          
         List<
    StringhostList = 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<Stringadapter
        
    = 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 0inetAddress.lengthi++){
     
         
    hostList.add(inetAddress[i].getClass() + " -\n"
           
    inetAddress[i].getHostName() + "\n"
           
    inetAddress[i].getHostAddress());
           }
         
       } catch (
    UnknownHostException e) {
     
        
    e.printStackTrace();
         
        
    error true;
        
    error_info e.toString();
       }
     
         }
        }
    }
    PHP:
    <?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>
     
    Đang tải...
  2. takeshi90 Moderator

    takeshi90

    Tham gia ngày:
    27/12/12
    Bài viết:
    153
    Đã được thích:
    78
    Điểm thành tích:
    0
    Giới tính:
    Nam
    mình làm giống bạn nhưng, khi click button test thì toàn báo errror "no address associated with host name". Mình test trên Galaxy Tab 2.
     

Chia sẻ trang này