Android利用WebService查询手机号码归属地
16lz
2021-01-23
Android利用WebService查询手机号码归属地
分类:Android 2013-10-03 22:45 57人阅读 评论(0) 收藏 举报MainActivity如下:
[java] view plain copy- packagecc.testwebservice;
- importjava.io.ByteArrayOutputStream;
- importjava.io.InputStream;
- importjava.io.OutputStream;
- importjava.net.HttpURLConnection;
- importjava.net.URL;
- importorg.apache.http.HttpStatus;
- importorg.xmlpull.v1.XmlPullParser;
- importandroid.os.Bundle;
- importandroid.util.Xml;
- importandroid.app.Activity;
- /**
- *Demo描述:
- *利用WebService查询手机号码归属地
- *
- *注意事项:
- *在http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
- *的SOAP1.2中明确提示请求的设置:
- *POST/WebServices/MobileCodeWS.asmxHTTP/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字段及其值
- *
- *参考文档:
- *1http://www.webxml.com.cn/zh_cn/index.aspx
- *2http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
- *3http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
- *
- *备注说明:
- *该服务在少数时候,会访问失败403错误.
- *多试几次即可
- */
- publicclassMainActivityextendsActivity{
- privatefinalStringmobileNumber="1500280";
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- newThread(){
- publicvoidrun(){
- Stringcity=queryMobileCodeByWebService(mobileNumber);
- System.out.println("city="+city);
- };
- }.start();
- }
- privateStringqueryMobileCodeByWebService(StringmobileNumber){
- Stringcity=null;
- try{
- InputStreaminputStream=this.getAssets().open("soap.xml");
- byte[]soapData=inputStreamToByteArray(inputStream);
- StringsoapContent=newString(soapData,"UTF-8");
- //替换soap.xml中的占位符$number
- soapContent=soapContent.replaceAll("\\$number",mobileNumber);
- byte[]entity=soapContent.getBytes();
- StringwebServicePath="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
- URLwebServiceURL=newURL(webServicePath);
- HttpURLConnectionhttpURLConnection=(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));
- OutputStreamoutputStream=httpURLConnection.getOutputStream();
- //发送数据
- outputStream.write(entity);
- if(httpURLConnection.getResponseCode()==HttpStatus.SC_OK){
- city=parseSOAPResponse(httpURLConnection.getInputStream());
- }
- }catch(Exceptione){
- }
- returncity;
- }
- //解析服务器以XML形式返回的SOAP
- publicStringparseSOAPResponse(InputStreaminputStream){
- Stringcity=null;
- try{
- XmlPullParserxmlPullParser=Xml.newPullParser();
- xmlPullParser.setInput(inputStream,"UTF-8");
- inteventType=xmlPullParser.getEventType();
- while(eventType!=XmlPullParser.END_DOCUMENT){
- switch(eventType){
- caseXmlPullParser.START_TAG:
- if("getMobileCodeInfoResult".equals(xmlPullParser.getName())){
- city=xmlPullParser.nextText();
- returncity;
- }
- break;
- }
- eventType=xmlPullParser.next();
- }
- }catch(Exceptione){
- }
- returncity;
- }
- publicbyte[]inputStreamToByteArray(InputStreaminputStream){
- ByteArrayOutputStreambyteArrayOutputStream=null;
- try{
- byteArrayOutputStream=newByteArrayOutputStream();
- byte[]buffer=newbyte[1024];
- intlen=0;
- while((len=inputStream.read(buffer))!=-1){
- byteArrayOutputStream.write(buffer,0,len);
- }
- byteArrayOutputStream.close();
- inputStream.close();
- }catch(Exceptione){
- }
- returnbyteArrayOutputStream.toByteArray();
- }
- }
main.xml如下:
- <RelativeLayoutxmlns: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如下:
- <?xmlversion="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>
- <getMobileCodeInfoxmlns="http://WebXml.com.cn/">
- <mobileCode>$number</mobileCode>
- <userID></userID>
- </getMobileCodeInfo>
- </soap12:Body>
- </soap12:Envelope>
更多相关文章
- 【Android】基于XMAPP协议实现Android推送服务(亲测可用)
- android获取手机号码以及imsi信息
- android常用数据库字段描述
- Android 模拟HTTP协议的编码问题 Android默认编码UTF-8
- 采用XMPP协议实现Android推送