導航:首頁 > 編程語言 > 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

友情鏈接