标签:
思路:先整理好所有域名后辍的whois主机地址,把他做成xml打包进APP,查域名的时候获取这个XML对应的主机地址,再用socket连接到这个地址,发送/接收信息(这个XML文件在assets下)。
先上截图:
(这个whois.xml是我十年前做的,我发了很长时间收集了大部分可靠的WHOIS主机,在这里这个文件才最有价值)
=============================================================================================================================================
MainActivity代码:
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 final int DEFAULT_PORT = 43; TextView tv1; String strinfo; EditText txtdomain; //装载Beauty类型的链表,其内容由XML文件解析得到 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); //开始按钮 Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //提示框,这样写在WINFORM是动画是动不了的,但Activity可以 pd = ProgressDialog.show(MainActivity.this, "请稍等", "正在从whois主机获取信息"); //启动线程 mHandler(txtdomain.getText().toString()); } }); } //获取XML里的WHOIS主机 private String get_whois_xml_server(String domain) { String result = ""; String[] arr; String tld=""; String serv=""; try { //通过assertmanager的open方法获取到beauties.xml文件的输入流 InputStream is = getResources().getAssets().open("whois.xml"); // 这个文件在assets下 //初始化自定义的实现类BeautyParserImpl BeautyParserImpl pbp = new BeautyParserImpl(); //调用pbp的parse()方法,将输入流传进去解析,返回的链表结果赋给beautyList beautyList = pbp.parse(is); } catch (Exception e) { e.printStackTrace(); } for (Beauty b : beautyList) { result = b.toString(); //看Beauty.java文件,arr[0]是域名后辍,arr[1]就是主机名 arr=result.split("==="); //在JAVA中,判断字符串相等要用equals,而不是像其它程序那样用==,习惯害死人 if(getTLD(domain).equals(arr[0].toString())){ serv =arr[1].toString(); break; } } return serv; } //子线程,当接收到WHOIS信息就关半那个提示框 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();// 关闭ProgressDialog } }; thread.start(); thread = null; } //更新UI final Handler handler2 = new Handler() { @Override public void handleMessage(Message msg) { tv1.setText(String.valueOf(msg.obj.toString())); } }; //用Socket连接到WHOIS服务器获取信息 private String getwhois(String domain) { try { //获取xml里的whois地址 String whoisaddress = get_whois_xml_server(domain); Log.i("whois_server=======",whoisaddress.toString()); //创建socket,并指定主机和端口 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(); } } //获取域名后辍 private String getTLD(String domain) { final int index; return (domain == null || (index = domain.lastIndexOf(‘.‘) + 1) < 1) ? domain : (index < (domain.length())) ? domain.substring(index) : ""; } // 读取assets文件 /* 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 action 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 will // 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); } }
总结:
1、socket 肯定是不能运行在主线程上的,所以要创建一个新线程来运行socket
2、android解析XML好麻烦,不懂的同学请复制网上实例(我是直接从网上复制的) http://www.cnblogs.com/devinzhang/archive/2012/01/16/2323668.html
3、按习惯我都是把XML直接转成datatable,,,,可JAVA没有datatable,很郁闷,很不习惯。
4、不知为何,,,JAVA要不断的try,否则编译不过
项目源文件下载:http://files.cnblogs.com/files/LCX/whois.rar
APP下载:http://files.cnblogs.com/files/LCX/whois.apk
有兴趣的同学可以下载到真机测试
android 基于 socket 的Whois域名查询,,整个项目打包下载
标签:
原文地址:http://www.cnblogs.com/LCX/p/4489211.html