[b]生活中经常遇到要根据手机号码查询手机的归属地或者其他信息,如下所示:[/b]
省份:浙江
城市:杭州
区号:0571
邮编:310000
移动公司名称:中国移动
卡类型:GSM
为此,本人闲得蛋疼的就写了段Java代码,通过网上现有的手机解析API获取手机详细信息。
[b]设计思想:[/b]
用百度搜索了下,网上有个网址有API可以提供,详细信息见连接[url]http://api.showji.com/Locating/help.htm#IFRAME[/url]
本人是用HttpClient基于超链接的形式进行查询,例如[url]http://guishu.showji.com/search.htm?m=13900008888[/url]
[b]下面详细解释下代码:[/b]
首先,我定义了一个手机对象,用来存储手机信息
package com.zhengtian.tools.bean.phone;
/**
* $Id: $
*
* @author zhengtian
* @time 2012-12-26
*/
public class MobilePhone {
// 移动手机号码
private long phone_number;
// 省份
private String province;
// 城市
private String city;
// 城市区号
private String city_code;
// 城市邮编
private long zip_code;
// 卡类型
private String card_type;
// 移动公司名称
private String corporation_name;
/**
* 输出手机详细信息
*
* @return
*/
public String getPhoneInfo() {
StringBuilder info = new StringBuilder();
info.append("手机号码:").append(phone_number).append("\n");
info.append("省份:").append(province).append("\n");
info.append("城市:").append(city).append("\n");
info.append("区号:").append(city_code).append("\n");
info.append("邮编:").append(zip_code).append("\n");
info.append("移动公司名称:").append(corporation_name).append("\n");
info.append("卡类型:").append(card_type).append("\n");
return info.toString();
}
public long getPhone_number() {
return phone_number;
}
public void setPhone_number(long phone_number) {
this.phone_number = phone_number;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCity_code() {
return city_code;
}
public void setCity_code(String city_code) {
this.city_code = city_code;
}
public long getZip_code() {
return zip_code;
}
public void setZip_code(long zip_code) {
this.zip_code = zip_code;
}
public String getCard_type() {
return card_type;
}
public void setCard_type(String card_type) {
this.card_type = card_type;
}
public String getCorporation_name() {
return corporation_name;
}
public void setCorporation_name(String corporation_name) {
this.corporation_name = corporation_name;
}
}
接着,是用HttpClient对手机号码进行解析,附件中tools-0.0.1-SNAPSHOT-sources.jar是源代码,tools_fat.jar是可执行jar包。phoneTools.rar是我自己用的Maven工程,有兴趣的朋友们,可以直接导入后查看代码和运行。如果朋友们需要重新打可运行的jar包,可以参考一下链接[url]http://zheng12tian.iteye.com/blog/1765626[/url]
下面讲解下解析手机号码的类,如下所示:
package com.zhengtian.tools.service.phone;
import java.io.IOException;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;
import com.zhengtian.tools.bean.phone.MobilePhone;
import com.zhengtian.tools.util.JsonUtils;
/**
* 手机信息查询工具类,主要实现根据手机号码查询手机归属地等信息。
*
* @author zhengtian
* @time 2012-12-26
*/
public class MobilePhoneTool {
/**
* 手机信息查询全局变量
*/
// 手机信息查询网址
private static final String URL = "http://api.showji.com/Locating/www.showji.com.aspx";
/**
* 上网代理全局变量
*/
// 代理Host
private static final String proxyHost = "10.10.10.78";
// 代理端口
private static final int proxyProt = 8080;
// 代理用户名
private static final String proxyUserName = "zhengtian";
// 代理密码
private static final String proxyPassword = "4rfv.com";
/**
* 响应包编码方式
*/
private static final String responseCharset = "UTF-8";
private MobilePhone getMobilePhone(long phone_number) {
HttpClient client = new HttpClient();
// 设置代理
//setProxy(client, proxyHost, proxyProt, proxyUserName, proxyPassword);
// get方式
HttpMethod method = new GetMethod(URL);
try {
// 设置get参数
NameValuePair m = new NameValuePair("m", String.valueOf(phone_number));
NameValuePair output = new NameValuePair("output", "json");
method.setQueryString(new NameValuePair[] { m, output });
// 服务器返回的状态(200:ok)
int statusCode = client.executeMethod(method);
if (statusCode == 200) {
// 服务器返回的信息
String responseBody = new String(method.getResponseBody(), responseCharset);
if (StringUtils.isNotEmpty(responseBody)) {
JSONObject json = JSONObject.fromObject(responseBody);
return convertJsonToMobilePhone(json);
}
System.out.println("查询结果为空");
} else {
System.out.println("查询失败");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放连接
method.releaseConnection();
}
return null;
}
/**
* 将json对象转换为MobilePhone
*
* @param json
* @return
*/
private MobilePhone convertJsonToMobilePhone(JSONObject json) {
if (JsonUtils.isNotEmpty(json)) {
MobilePhone phone = new MobilePhone();
// 获取手机号码
long mobile = JsonUtils.getLongValue(json, "Mobile");
phone.setPhone_number(mobile);
// 获取省份
String province = JsonUtils.getStringValue(json, "Province");
phone.setProvince(province);
// 获取城市
String city = JsonUtils.getStringValue(json, "City");
phone.setCity(city);
// 获取城市区号
String areacode = JsonUtils.getStringValue(json, "AreaCode");
phone.setCity_code(areacode);
// 获取邮编
long postcode = JsonUtils.getLongValue(json, "PostCode");
phone.setZip_code(postcode);
// 获取公司
String corp = JsonUtils.getStringValue(json, "Corp");
phone.setCorporation_name(corp);
// 获取sim卡类型
String card = JsonUtils.getStringValue(json, "Card");
phone.setCard_type(card);
return phone;
}
return null;
}
/**
* 设置代理
*
* @param client
* @param proxyHost
* @param proxyProt
* @param userName
* @param password
*/
private void setProxy(HttpClient client, String proxyHost, int proxyProt, String userName, String password) {
// 设置HTTP代理IP和端口
client.getHostConfiguration().setProxy(proxyHost, proxyProt);
// 代理认证
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName, password);
client.getState().setProxyCredentials(AuthScope.ANY, creds);
}
public static void main(String[] args) {
long no = args != null && args.length > 0 ? Long.valueOf(args[0]) : 18658155313L;
if (args != null && args.length > 1) {
no = Long.valueOf(args[0]);
}
MobilePhoneTool phone = new MobilePhoneTool();
MobilePhone info = phone.getMobilePhone(no);
if (info != null) {
System.out.println(info.getPhoneInfo());
}
}
}
如果机器上有jdk的环境,那么可以直接用cmd运行tools_fat.jar,详细用法如下
[img]http://dl.iteye.com/upload/attachment/0078/9951/a523a560-9057-3177-acd1-5c4870baee25.jpg[/img]