Android socket-based WHOIS domain name query, the entire project package download

Source: Internet
Author: User

Idea: first tidy up all the domain name after the WHOIS host address, put him into the XML package into the app, check the domain name to get this XML corresponding host address, and then connect to this address with the socket, send/Receive information (this XML file under assets).

First on:

(This whois.xml is what I did ten years ago, and I've spent a lot of time collecting most of the reliable Whois hosts, where this file is the most valuable )

=============================================================================================================== ==============================

Mainactivity Code:

Package Cn.net.vb.whois.whois;import Android.app.progressdialog;import Android.os.handler;import Android.os.message;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.util.log;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;import Java.io.BufferedReader ; Import Java.io.ioexception;import Java.io.inputstream;import java.io.inputstreamreader;import java.io.PrintWriter Import Java.net.socket;import Java.util.list;public class Mainactivity extends Actionbaractivity {private static FINA    l int default_port = 43;    TextView TV1;    String Strinfo;    EditText Txtdomain;    Load Beauty type of linked list, whose contents are parsed by XML file to get private list<beauty> beautylist;    Private ProgressDialog PD;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);     Setcontentview (R.layout.activity_main);   TV1 = (TextView) Findviewbyid (R.ID.TV1);        Txtdomain = (EditText) Findviewbyid (R.id.txtdomain);        Start button button1 = (button) Findviewbyid (R.id.button1);                Button1.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { The prompt box, which is written in WinForm is animation is not moving, but activity can be PD = Progressdialog.show (mainactivity.this, "Please Wait", "is going from the WHO                is host to obtain information ");            Start Thread Mhandler (Txtdomain.gettext (). toString ());    }        });        }//Gets the WHOIS host in XML private string Get_whois_xml_server (string domain) {string result = "";        String[] arr;        String tld= "";        String serv= ""; try {///through Assertmanager's Open method gets the input stream to the Beauties.xml file InputStream is = Getresources (). Getassets (). Open ("Whois.xml");            This file is under assets//initialization of the custom implementation class Beautyparserimpl Beautyparserimpl PBP = new Beautyparserimpl (); Call PBP's ParsE () method, the input stream into the parse, the returned list of results assigned to beautylist beautylist = Pbp.parse (IS);        } catch (Exception e) {e.printstacktrace ();            } for (Beauty b:beautylist) {result = B.tostring ();            See Beauty.java file, Arr[0] is the domain name suffix, arr[1] is the hostname arr=result.split ("= = ="); In Java, it is customary to use equals to determine the string equality, rather than to use = = as in other programs, to kill if (gettld (domain)). Equals (Arr[0].tostring ())) {Serv                =arr[1].tostring ();            Break    }} return serv;            }//Sub-thread, when receiving WHOIS information, close the box private void Mhandler (final String strname) {Thread thread = new Thread () {                @Override public void Run () {Message message = Handler2.obtainmessage ();                MESSAGE.ARG1 = 1;                Message.obj = Getwhois (strname). ToString ();                Handler2.sendmessage (message);        Pd.dismiss ();//Close ProgressDialog}};        Thread.Start (); Thread= NULL;            }//Update UI final Handler handler2 = new Handler () {@Override public void Handlemessage (Message msg) {        Tv1.settext (String.valueof (msg.obj.toString ()));    }    }; Connect to the WHOIS server with a socket to get information private string Getwhois (string domain) {try {///Get the WHOIS address in XML           Ring whoisaddress = get_whois_xml_server (domain);            LOG.I ("whois_server=======", whoisaddress.tostring ());            Create a socket and specify the host and port socket socket = new socket (whoisaddress, default_port);            String lineseparator = "\ r \ n";            PrintWriter out = new PrintWriter (Socket.getoutputstream ());            OUT.PRINTLN (domain);            Out.flush ();            BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));            StringBuilder ret = new StringBuilder ();            String Line;            while (line = In.readline ())! = null) {Ret.append (line + lineseparator);} socket.close ();            LOG.I ("whois_info======:", ret.tostring ());        return ret.tostring ();            } catch (IOException e) {e.printstacktrace ();        Return E.getmessage (). toString ();        }}//Gets the domain name suffix private string gettld (string domain) {final int index; return (domain = = NULL | | (index = Domain.lastindexof ('. ') + 1) < 1)? Domain: (Index < (domain.length))?    Domain.substring (Index): "";        }//Read assets file/* private void Assetsread () {String fileName = "Whois.xml";        String ret = "";            try {InputStream is = Getresources (). Getassets (). open (FileName);            int len = is.available ();            byte []buffer = new Byte[len];            Is.read (buffer);            RET = encodingutils.getstring (buffer, "utf-8");        Is.close ();        } catch (Exception e) {e.printstacktrace (); } TV1 = (TextView) Findviewbyid (R.ID.TV1);       Tv1.settext (ret); }*/@Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Act        Ion Bar if it is present.        Getmenuinflater (). Inflate (R.menu.menu_main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}

  

Summarize:

1, socket must not run on the main thread, so to create a new thread to run the socket

2, Android parsing xml good trouble, do not understand the classmate please copy the online instance (I copied directly from the Internet) http://www.cnblogs.com/devinzhang/archive/2012/01/16/2323668.html

3, according to the habit I have to turn XML directly into a DataTable,,, Java no DataTable, very depressed, very unaccustomed.

4, do not know why,,, Java to keep the try, otherwise compiled but

Project source file Download: Http://files.cnblogs.com/files/LCX/whois.rar

App Download: http://files.cnblogs.com/files/LCX/whois.apk

Interested students can download to the real machine test

Android socket-based WHOIS domain name query, the entire project package download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.