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

分类:Android 57人阅读 评论(0) 收藏 举报

MainActivity如下:

[java] view plain copy
  1. packagecc.testwebservice;
  2. importjava.io.ByteArrayOutputStream;
  3. importjava.io.InputStream;
  4. importjava.io.OutputStream;
  5. importjava.net.HttpURLConnection;
  6. importjava.net.URL;
  7. importorg.apache.http.HttpStatus;
  8. importorg.xmlpull.v1.XmlPullParser;
  9. importandroid.os.Bundle;
  10. importandroid.util.Xml;
  11. importandroid.app.Activity;
  12. /**
  13. *Demo描述:
  14. *利用WebService查询手机号码归属地
  15. *
  16. *注意事项:
  17. *在http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
  18. *的SOAP1.2中明确提示请求的设置:
  19. *POST/WebServices/MobileCodeWS.asmxHTTP/1.1
  20. *Host:webservice.webxml.com.cn
  21. *Content-Type:application/soap+xml;charset=utf-8
  22. *Content-Length:length
  23. *即:
  24. *请求方法:POST采用HTTP协议路径为/WebServices/MobileCodeWS.asmx
  25. *主机名称:webservice.webxml.com.cn
  26. *所以请求路径为http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
  27. *亦明确提示在POST请求时需要设置:
  28. *Content-Type和Content-Length字段及其值
  29. *
  30. *参考文档:
  31. *1http://www.webxml.com.cn/zh_cn/index.aspx
  32. *2http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
  33. *3http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
  34. *
  35. *备注说明:
  36. *该服务在少数时候,会访问失败403错误.
  37. *多试几次即可
  38. */
  39. publicclassMainActivityextendsActivity{
  40. privatefinalStringmobileNumber="1500280";
  41. @Override
  42. protectedvoidonCreate(BundlesavedInstanceState){
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.main);
  45. newThread(){
  46. publicvoidrun(){
  47. Stringcity=queryMobileCodeByWebService(mobileNumber);
  48. System.out.println("city="+city);
  49. };
  50. }.start();
  51. }
  52. privateStringqueryMobileCodeByWebService(StringmobileNumber){
  53. Stringcity=null;
  54. try{
  55. InputStreaminputStream=this.getAssets().open("soap.xml");
  56. byte[]soapData=inputStreamToByteArray(inputStream);
  57. StringsoapContent=newString(soapData,"UTF-8");
  58. //替换soap.xml中的占位符$number
  59. soapContent=soapContent.replaceAll("\\$number",mobileNumber);
  60. byte[]entity=soapContent.getBytes();
  61. StringwebServicePath="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
  62. URLwebServiceURL=newURL(webServicePath);
  63. HttpURLConnectionhttpURLConnection=(HttpURLConnection)webServiceURL.openConnection();
  64. httpURLConnection.setConnectTimeout(8000);
  65. httpURLConnection.setRequestMethod("POST");
  66. //因为要发送SOAP协议,所以允许对外输出
  67. httpURLConnection.setDoOutput(true);
  68. //设置该SOAP协议要求的Content-Type字段
  69. httpURLConnection.setRequestProperty("Content-Type","application/soap+xml;charset=utf-8");
  70. //设置该SOAP协议要求的Content-Length字段
  71. httpURLConnection.setRequestProperty("Content-Length",String.valueOf(entity.length));
  72. OutputStreamoutputStream=httpURLConnection.getOutputStream();
  73. //发送数据
  74. outputStream.write(entity);
  75. if(httpURLConnection.getResponseCode()==HttpStatus.SC_OK){
  76. city=parseSOAPResponse(httpURLConnection.getInputStream());
  77. }
  78. }catch(Exceptione){
  79. }
  80. returncity;
  81. }
  82. //解析服务器以XML形式返回的SOAP
  83. publicStringparseSOAPResponse(InputStreaminputStream){
  84. Stringcity=null;
  85. try{
  86. XmlPullParserxmlPullParser=Xml.newPullParser();
  87. xmlPullParser.setInput(inputStream,"UTF-8");
  88. inteventType=xmlPullParser.getEventType();
  89. while(eventType!=XmlPullParser.END_DOCUMENT){
  90. switch(eventType){
  91. caseXmlPullParser.START_TAG:
  92. if("getMobileCodeInfoResult".equals(xmlPullParser.getName())){
  93. city=xmlPullParser.nextText();
  94. returncity;
  95. }
  96. break;
  97. }
  98. eventType=xmlPullParser.next();
  99. }
  100. }catch(Exceptione){
  101. }
  102. returncity;
  103. }
  104. publicbyte[]inputStreamToByteArray(InputStreaminputStream){
  105. ByteArrayOutputStreambyteArrayOutputStream=null;
  106. try{
  107. byteArrayOutputStream=newByteArrayOutputStream();
  108. byte[]buffer=newbyte[1024];
  109. intlen=0;
  110. while((len=inputStream.read(buffer))!=-1){
  111. byteArrayOutputStream.write(buffer,0,len);
  112. }
  113. byteArrayOutputStream.close();
  114. inputStream.close();
  115. }catch(Exceptione){
  116. }
  117. returnbyteArrayOutputStream.toByteArray();
  118. }
  119. }


main.xml如下:

[html] view plain copy
  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. >
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="利用WebService查询手机号码归属地"
  10. android:layout_centerInParent="true"
  11. />
  12. </RelativeLayout>


soap.xml如下:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <soap12:Envelope
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  5. xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  6. <soap12:Body>
  7. <getMobileCodeInfoxmlns="http://WebXml.com.cn/">
  8. <mobileCode>$number</mobileCode>
  9. <userID></userID>
  10. </getMobileCodeInfo>
  11. </soap12:Body>
  12. </soap12:Envelope>


更多相关文章

  1. 【Android】基于XMAPP协议实现Android推送服务(亲测可用)
  2. android获取手机号码以及imsi信息
  3. android常用数据库字段描述
  4. Android 模拟HTTP协议的编码问题 Android默认编码UTF-8
  5. 采用XMPP协议实现Android推送

随机推荐

  1. 解决m1 MacBook没有我的照片流功能
  2. Grafana 之 自定义监控板
  3. Grafana 之 kubeGraf插件安装使用
  4. 用ldap作为django后端用户登录验证
  5. 记一次生产环境问题解决案例(k8s环境)
  6. ArrayList底层
  7. 苹果Mac强大的图床软件:​​​​PicGo
  8. 【东哥说书】俞军产品方法论
  9. Apache Flink 商业公司 Ververica 又有几
  10. 一文了解 Apache Hive 联邦查询(Query Fed