导航:首页 > 编程语言 > java获取手机归属地

java获取手机归属地

发布时间:2025-06-15 09:24:15

A. 我新买到三星双卡双待手机,支持java格式,我想找一款类似来电通的软件,只要有来电归属地提示就行。

楼上纯属扯淡 JAVA没有来去电归属地显示
因为非智能手机不支持软件后台运行的
不过三星W系列的双卡手机 工具里有个来电信息 你勾上就可以了 我的W589就有的

B. java系统怎么设置号码归属地

java中利用爬虫的思路去完成这个功能。这里大概思路是通过HttpClient去模拟提交那些网站的查询功能,这里是www.ip138.com,然后通过正则表达式去解析HttpClient相应内容,从里面抽取出手机归属地。同时对要查询的手机进行一个验证,具体代码请看如下:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* 通过手机号码,获得该号码的归属地
*
* @author Administrator
*
*/
public class MobileFromUtil {
//正则表达式,抽取手机归属地
public static final String REGEX_GET_MOBILE=
"(?is)(<tr[^>]+>[\\s]*<td[^>]+>[\\s]*卡号归属地[\\s]*</td>[\\s]*<td[^>]+>([^<]+)</td>[\\s]*</tr>)"; //2:from
//正则表达式,审核要获取手机归属地的手机是否符合格式,可以只输入手机号码前7位
public static final String REGEX_IS_MOBILE=
"(?is)(^1[3|4|5|8][0-9]\\d{4,8}$)";

/**
* 获得手机号码归属地
*
* @param mobileNumber
* @return
* @throws Exception
*/
public static String getMobileFrom(String mobileNumber) throws Exception {
if(!veriyMobile(mobileNumber)){
throw new Exception("不是完整的11位手机号或者正确的手机号前七位");
}
HttpClient client=null;
PostMethod method=null;
NameValuePair mobileParameter=null;
NameValuePair actionParameter=null;
int httpStatusCode;

String htmlSource=null;
String result=null;

try {
client=new HttpClient();
client.getHostConfiguration().setHost("www.ip138.com", 8080, "http");
method=new PostMethod("/search.asp");
mobileParameter=new NameValuePair("mobile",mobileNumber);
actionParameter=new NameValuePair("action","mobile");
method.setRequestBody(new NameValuePair[] { actionParameter,mobileParameter });
//设置编码
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GB2312");

client.executeMethod(method);
httpStatusCode=method.getStatusLine().getStatusCode();
if(httpStatusCode!=200){
throw new Exception("网页内容获取异常!Http Status Code:"+httpStatusCode);
}

htmlSource=method.getResponseBodyAsString();
if(htmlSource!=null&&!htmlSource.equals("")){
result=parseMobileFrom(htmlSource);
}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
method.releaseConnection();
}

return result;

}

/**
* 从www.ip138.com返回的结果网页内容中获取手机号码归属地,结果为:省份 城市
*
* @param htmlSource
* @return
*/
public static String parseMobileFrom(String htmlSource){
Pattern p=null;
Matcher m=null;
String result=null;

p=Pattern.compile(REGEX_GET_MOBILE);
m=p.matcher(htmlSource);

while(m.find()){
if(m.start(2)>0){
result=m.group(2);
result=result.replaceAll(" ", " ");
}
}

return result;

}

/**
* 验证手机号
* @param mobileNumber
* @return
*/
public static boolean veriyMobile(String mobileNumber){
Pattern p=null;
Matcher m=null;

p=Pattern.compile(REGEX_IS_MOBILE);
m=p.matcher(mobileNumber);

return m.matches();
}

/**
* 测试
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(getMobileFrom("13888888888"));
}
}

阅读全文

与java获取手机归属地相关的资料

热点内容
苹果实体店beats 浏览:143
全频道电视直播app有哪些 浏览:863
医疗app的slogan 浏览:792
wps怎么恢复删除文件 浏览:610
ug数车编程如何不让刀路往下跑 浏览:548
怎么把dat文件转换 浏览:899
海总的app 浏览:701
数据结构是数据的什么结构 浏览:564
qq明星背景图片大全 浏览:681
苹果手机底部导航栏不见了 浏览:255
美团app扫码单车完了怎么使用 浏览:784
怎么设置文件夹的权限 浏览:901
文件印刷工作 浏览:832
荔湾网络布线有哪些 浏览:119
缓存视频文件分段合并 浏览:34
js如何创建http请求 浏览:531
华北强手表app用哪个 浏览:682
乐视网络电视升级取消 浏览:689
压缩文件搜索 浏览:540
mysql忘记密码 浏览:826

友情链接