import java.io.InputStream
import java.io.OutputStream
import java.io.OutputStreamWriter
import java.net.URL
import java.net.URLConnection
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import org.w3c.dom.NodeList
public class Mobile {
private static String getSoapRequest(String mobileCode) {
StringBuilder sb = new StringBuilder()
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "\n"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ " "
+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
+ " "
+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "\n"
+ "<soap:Body>" + "\n"
+ "<getMobileCodeInfo" + " "
+ "xmlns=\"http://WebXml.com.cn/\">" + "\n"
+ "<mobileCode>" + mobileCode + "</mobileCode>" + "\n"
+ "<userID></userID>" + "\n"
+ "</getMobileCodeInfo>" + "\n"
+ "</soap:Body>" + "\n"
+ "</soap:Envelope>"
)
return sb.toString()
}
private static InputStream getSoapInputStream(String mobileCode) {
try {
String soap = getSoapRequest(mobileCode)
if (soap == null)
return null
URL url = new URL(
"http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx")
URLConnection conn = url.openConnection()
conn.setUseCaches(false)
conn.setDoInput(true)
conn.setDoOutput(true)
conn.setRequestProperty("Content-Type", "text/xml charset=utf-8")
conn.setRequestProperty("Content-Length", Integer.toString(soap
.length()))
conn.setRequestProperty("SOAPAction",
"http://WebXml.com.cn/getMobileCodeInfo")
OutputStream os = conn.getOutputStream()
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8")
osw.write(soap)
osw.flush()
osw.close()
InputStream is = conn.getInputStream()
return is
} catch (Exception e) {
e.printStackTrace()
return null
}
}
public static String getMobileNoTrack(String mobileCode) {
try {
org.w3c.dom.Document document = null
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance()
dbf.setNamespaceAware(true)
InputStream is = getSoapInputStream(mobileCode)
DocumentBuilder db = dbf.newDocumentBuilder()
document = db.parse(is)
NodeList nl = document
.getElementsByTagName("getMobileCodeInfoResult")
StringBuffer sb = new StringBuffer()
for (int i = 0 i < nl.getLength() i++) {
org.w3c.dom.Node n = nl.item(i)
if (n.getFirstChild().getNodeValue().equals("手机号码错误")) {
sb = new StringBuffer("#")
System.out.println("手机号码输入有误")
break
}
sb.append(n.getFirstChild().getNodeValue() + "\n")
}
is.close()
return sb.toString()
} catch (Exception e) {
e.printStackTrace()
return null
}
}
public static void main(String[] args) {
// System.out.println(Moblie.getSoapRequest("13272303204"))
// System.out.println(Moblie.getSoapInputStream("13226678785"))
System.out.println(Mobile.getMobileNoTrack("1583759999"))
}
}
这段代码也是我在网上找的,然后在Myeclipse8.5M2中测试通过的根据输入的电话号码,可以查询号码归属地.不知道是否你需要的答案...
手机号码归属地接口:根据手机号码或手机号码的前7位,查询手机号码归属地信息,包括省份 、城市、区号、邮编、运营商和卡类型。
接口文档: https://www.juhe.cn/docs/api/id/11
如要使用,则需要先申请APPKEY,通过审核,之后就可以无限次免费使用。
接口地址: http://apis.juhe.cn/mobile/get
支持格式:json/xml
请求方式:get
请求示例: http://apis.juhe.cn/mobile/get?phone=13429667914&key= 您申请的KEY
调用样例及调试工具: API测试工具
请求参数说明:
返回参数说明:
JSON返回示例:
XML返回示例:
系统级错误码参照:
错误码格式说明(示例:200201):
PHP: 手机号码归属地查询
Python: 手机号码归属地接口调用示例
C#: 手机号码归属地接口调用示例
Go: 手机号码归属地接口调用示例
JAVA: 手机号码归属地接口调用示例
手机上带有来电归属地来电不显示来电地址原因有两个:1、您的手机是JAVA平台,该平台的来电归属地软件不支持来电地址显示功能。
2、您的是智能机的话,那么就是您的软件设置问题,也有可能是安全类软件把它禁止启动造成的。
希望帮到您!