手機歸屬地查詢

  • 手機歸屬地查詢工具,網絡版,要聯網查詢,上網費用自理。
  • CellPhoneLocation.java如下:
package com.ldq.cell;

import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class CellPhoneLocation extends MIDlet {
	private Display display;
	private Form form;
	private TextField text;
	private Command cmdOK;
	private Command cmdExit;

	private String ret;

	private void getInfo(String mobileCode, String userID) {
		String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="
				+ text.getString() + "&userID=";
		try {
			HttpConnection con = (HttpConnection) Connector.open(url);
			if (con.getResponseCode() == HttpConnection.HTTP_OK) {
				System.out.println("HTTP_OK");
				InputStream is = con.openInputStream();
				byte[] b = new byte[is.available()];
				is.read(b);
				ret = new String(b, "utf-8");
				System.out.println(ret);
				ret = ret.substring(ret.indexOf('>') + 1);
				ret = ret.substring(ret.indexOf('>') + 1);
				ret = ret.substring(0, ret.indexOf('<'));
				System.out.println(ret);
				is.close();
			} else {
				ret = "連接失敗!";
			}
			con.close();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	};

	private CommandListener listener = new CommandListener() {
		public void commandAction(Command arg0, Displayable arg1) {
			// TODO Auto-generated method stub
			if (arg0 == cmdOK) {
				new Thread() {
					public void run() {
						getInfo(text.getString(), "");
						form.delete(1);
						form.append(ret);
					}
				}.start();
			} else if (arg0 == cmdExit) {
				notifyDestroyed();
			}
		}
	};

	public CellPhoneLocation() {
		form = new Form("手機歸屬地查詢");
		text = new TextField("輸入手機號", "", 255, 0);
		cmdOK = new Command("確定", Command.ITEM, 0);
		cmdExit = new Command("退出", Command.EXIT, 0);
		form.append(text);
		form.append("手機歸屬地信息");
		form.addCommand(cmdOK);
		form.addCommand(cmdExit);
		form.setCommandListener(listener);
		// TODO Auto-generated constructor stub
	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		display = Display.getDisplay(this);
		display.setCurrent(form);
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}
}
  • GET()請求另外啓動線程處理,如果不這樣的話,直接死機了
  • 解析GET()獲得的數據採用爲本處理方式,比較笨,不得已爲之
  • 其實解析數據可以使用標準的 SAXParser ,在模擬器上運行通過,但是真機上安裝不了,原因未知
  • 使用 SAXParser 的代碼如下:
package com.ldq;

import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class CellPhone extends MIDlet {
	private Display display;
	private Form form;
	private TextField text;
	private Command cmdOK;
	private Command cmdExit;

	private String ret;

	private DefaultHandler handler = new DefaultHandler() {

		public void startElement(String uri, String localName, String name,
				Attributes attributes) throws SAXException {
			// TODO Auto-generated method stub
			System.out.println(name);
			super.startElement(uri, localName, name, attributes);
		}

		public void characters(char[] ch, int start, int length)
				throws SAXException {
			// TODO Auto-generated method stub
			System.out.println(new String(ch, start, length));
			ret = new String(ch, start, length);
			super.characters(ch, start, length);
		}

		public void endElement(String uri, String localName, String name)
				throws SAXException {
			// TODO Auto-generated method stub
			super.endElement(uri, localName, name);
		}
	};

	private void getInfo(String mobileCode, String userID) {
		String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="
				+ text.getString() + "&userID=";

		try {
			HttpConnection con = (HttpConnection) Connector.open(url);
			if (con.getResponseCode() == HttpConnection.HTTP_OK) {
				System.out.println("HTTP_OK");
				InputStream is = con.openInputStream();
				SAXParserFactory f = SAXParserFactory.newInstance();
				SAXParser p = f.newSAXParser();
				p.parse(is, handler);
				is.close();
			} else {
				ret = "連接失敗!";
			}
			con.close();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	};

	private CommandListener listener = new CommandListener() {
		public void commandAction(Command arg0, Displayable arg1) {
			// TODO Auto-generated method stub
			if (arg0 == cmdOK) {
				new Thread() {
					public void run() {
						getInfo(text.getString(), "");
						form.delete(1);
						form.append(ret);
					}
				}.start();
			} else if (arg0 == cmdExit) {
				notifyDestroyed();
			}
		}
	};

	public CellPhone() {
		form = new Form("手機歸屬地查詢");
		text = new TextField("輸入手機號", "", 255, 0);
		cmdOK = new Command("確定", Command.ITEM, 0);
		cmdExit = new Command("退出", Command.EXIT, 0);
		form.append(text);
		form.append("手機歸屬地信息");
		form.addCommand(cmdOK);
		form.addCommand(cmdExit);
		form.setCommandListener(listener);
		// TODO Auto-generated constructor stub
	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		display = Display.getDisplay(this);
		display.setCurrent(form);
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}
}

 安裝了Java虛擬機的Android手機上運行結果如下:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章