Android利用WebService查询手机号码归属地

简介: MainActivity如下: package cc.testwebservice;import java.io.ByteArrayOutputStream;import java.

MainActivity如下:

package cc.testwebservice;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpStatus;
import org.xmlpull.v1.XmlPullParser;
import android.os.Bundle;
import android.util.Xml;
import android.app.Activity;
/**
 * Demo描述:
 * 利用WebService查询手机号码归属地
 * 
 * 注意事项:
 * 在http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
 * 的SOAP 1.2中明确提示请求的设置:
 * POST /WebServices/MobileCodeWS.asmx HTTP/1.1
 * Host: webservice.webxml.com.cn
 * Content-Type: application/soap+xml; charset=utf-8
 * Content-Length: length
 * 即:
 * 请求方法:POST 采用HTTP协议 路径为/WebServices/MobileCodeWS.asmx
 * 主机名称:webservice.webxml.com.cn
 * 所以请求路径为http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
 * 亦明确提示在POST请求时需要设置:
 * Content-Type和Content-Length字段及其值
 * 
 * 参考文档:
 * 1 http://www.webxml.com.cn/zh_cn/index.aspx
 * 2 http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
 * 3 http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
 * 
 * 备注说明:
 * 该服务在少数时候,会访问失败403错误.
 * 多试几次即可
 */
public class MainActivity extends Activity {
    private final String mobileNumber="1500280";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		new Thread(){
			public void run() {
				String city=queryMobileCodeByWebService(mobileNumber);
				System.out.println("city="+city);
			};
		}.start();
		
	}
    private String queryMobileCodeByWebService(String mobileNumber){
    	String city=null;
    	try {
    		InputStream inputStream=this.getAssets().open("soap.xml");
    		byte [] soapData=inputStreamToByteArray(inputStream);
    		String soapContent=new String(soapData, "UTF-8");
    		//替换soap.xml中的占位符$number
    		soapContent = soapContent.replaceAll("\\$number", mobileNumber);
    		byte [] entity=soapContent.getBytes();
    		
    		String webServicePath = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
    		URL webServiceURL=new URL(webServicePath);
    		HttpURLConnection httpURLConnection = (HttpURLConnection) webServiceURL.openConnection();
    		httpURLConnection.setConnectTimeout(8000);
    		httpURLConnection.setRequestMethod("POST");
    		//因为要发送SOAP协议,所以允许对外输出
    		httpURLConnection.setDoOutput(true);
    		//设置该SOAP协议要求的Content-Type字段
    		httpURLConnection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
    		//设置该SOAP协议要求的Content-Length字段
    		httpURLConnection.setRequestProperty("Content-Length", String.valueOf(entity.length));
    		OutputStream outputStream = httpURLConnection.getOutputStream();
    		//发送数据
    		outputStream.write(entity);
    		if (httpURLConnection.getResponseCode()==HttpStatus.SC_OK) {
				city=parseSOAPResponse(httpURLConnection.getInputStream());
			}
		} catch (Exception e) {
			
		}
    	
		return city;
    }
    
    //解析服务器以XML形式返回的SOAP
    public String parseSOAPResponse(InputStream inputStream) {
		String city = null;
		try {
			XmlPullParser xmlPullParser = Xml.newPullParser();
			xmlPullParser.setInput(inputStream, "UTF-8");
			int eventType = xmlPullParser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_TAG:
					if ("getMobileCodeInfoResult".equals(xmlPullParser.getName())) {
						city = xmlPullParser.nextText();
						return city;
					}
					break;
				}
				eventType = xmlPullParser.next();
			}
		} catch (Exception e) {
			
		}

		return city;
	}
    
   
    public  byte[] inputStreamToByteArray(InputStream inputStream){
    	ByteArrayOutputStream byteArrayOutputStream=null;
    	try {
    		byteArrayOutputStream = new ByteArrayOutputStream();
    		byte[] buffer = new byte[1024];
    		int len = 0;
    		while( (len= inputStream.read(buffer)) != -1 ){
    			byteArrayOutputStream.write(buffer, 0, len);
    		}
    		byteArrayOutputStream.close();
    		inputStream.close();
		} catch (Exception e) {
			
		}
		return byteArrayOutputStream.toByteArray();
	}
	
}


main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="利用WebService查询手机号码归属地"
        android:layout_centerInParent="true"
     />

</RelativeLayout>


soap.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfo xmlns="http://webxml.com.cn/">
      <mobileCode>$number</mobileCode>
      <userID></userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>


 

相关文章
|
5月前
|
安全 API Python
详解手机状态查询API实战指南
手机状态查询API是一款高效接口,可实时识别手机号状态(实号、空号、风险号等),帮助企业筛选有效号码,提升业务触达率与客户体验。
637 0
|
5月前
|
Android开发 数据安全/隐私保护
安卓手机修改设备id, 安卓硬改一键新机,手机机型修改(伪装)
提供了完整的设备信息修改功能,包含设备模板配置、基础信息修改、网络信息修改、模拟器检测绕
|
5月前
|
存储 人工智能 文字识别
三款安卓手机word编辑器下载,Microsoft Word,wps office,Word手机版,手机word编辑查看阅读器,PDF转换器apk下载
WPS Office是一款功能强大的办公软件,支持文档编辑、表格处理和演示文稿制作,兼容多种格式并提供丰富的云服务。它具备低内存占用、快速运行的特点,支持跨设备同步与多人协作,内置海量模板及AI辅助功能,如智能写作和PPT自动生成。此外,还可扫描文件、编辑PDF并转换为其他格式,极大提升办公效率,适合手机用户便捷操作。
599 1
|
6月前
|
Android开发
安卓硬改一键新机工具,一键修改手机型号,串号网卡Imei、sn码【仅供学习参考】
声明部分:仅供学习参考使用,基于Xposed框架实现的设备信息伪装模块的完整代码,包含多个功能模块:
|
5月前
|
API Android开发 数据安全/隐私保护
|
6月前
|
存储 JSON API
安卓ck提取工具,可提取手机cookie插件,AUTOJS即可实现
怎么用autojs提取手机端的CK?其实autojs是支持提取ck的但是他提取的不是浏览器的CK,二十他自身浏览器环境的c
|
6月前
|
Java Android开发
安卓手机硬改工具, 设备型号修改神器, 安卓硬改一键新机
通过Java创建可执行JAR来修改安卓设备信息。核心功能包括读取系统属性
|
6月前
|
存储 Android开发
一键新机安卓无限, 免root改手机机型, 手机信息修改型号伪装
AndroidManifest.xml配置 资源文件管理 各系统服务的Hook
|
11月前
|
移动开发 数据安全/隐私保护
ClKLog支持手机端查询统计数据啦!
ClKLog的付费版中提供了兼容移动端的h5展示界面,简单来说,手机浏览器直接访fangwe问统计地址就能直接查询主要的统计数据。
|
Android开发 数据安全/隐私保护 虚拟化
安卓手机远程连接登录Windows服务器教程
安卓手机远程连接登录Windows服务器教程
2710 5

热门文章

最新文章