- 手机归属地查询工具,网络版,要联网查询,上网费用自理。
- 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手机上运行结果如下: