需要使用到WebService的服务,这里选择www.webxml.com.cn提供的服务来查询电话号码归属地,使用方法网页上有介绍,这里使用一个实例来演示如何在Android下实现电话号码归属地的查询:

0.使用webxml的soap方式:mobilesoap.xml在src目录下

view plain
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <soap12:Envelopexmlns: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">
  3. <soap12:Body>
  4. <getMobileCodeInfoxmlns="http://WebXml.com.cn/">
  5. <mobileCode>$mobile</mobileCode>
  6. <userID></userID>
  7. </getMobileCodeInfo>
  8. </soap12:Body>
  9. </soap12:Envelope>

1.MainActivity.java

view plain
  1. publicclassMainActivityextendsActivity
  2. {
  3. privateEditTextmobileText;
  4. privateTextViewaddressView;
  5. privatestaticfinalStringTAG="MainActivity";
  6. @Override
  7. publicvoidonCreate(BundlesavedInstanceState)
  8. {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11. mobileText=(EditText)this.findViewById(R.id.mobile);
  12. addressView=(TextView)this.findViewById(R.id.address);
  13. Buttonbutton=(Button)this.findViewById(R.id.button);
  14. button.setOnClickListener(newView.OnClickListener()
  15. {
  16. @Override
  17. publicvoidonClick(Viewv)
  18. {
  19. Stringmobile=mobileText.getText().toString();
  20. InputStreaminStream=this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml");
  21. try
  22. {
  23. addressView.setText(MobileInfoService.getMobileAddress(inStream,mobile));
  24. }
  25. catch(Exceptione)
  26. {
  27. Log.e(TAG,e.toString());
  28. Toast.makeText(MainActivity.this,"查询失败",1).show();
  29. }
  30. }
  31. });
  32. }
  33. }

2.MobileInfoService.java

view plain
  1. publicclassMobileInfoService
  2. {
  3. /**
  4. *获得电话号码归属地信息
  5. *@paraminStream
  6. *@parammobile
  7. *@return
  8. *@throwsException
  9. */
  10. publicstaticStringgetMobileAddress(InputStreaminStream,Stringmobile)throwsException
  11. {
  12. //定义输入流
  13. Stringsoap=readSoapFile(inStream,mobile);
  14. byte[]data=soap.getBytes();
  15. URLurl=newURL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
  16. HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
  17. conn.setRequestMethod("POST");
  18. conn.setConnectTimeout(5*1000);
  19. //如果通过post提交数据,必须设置允许对外输出数据
  20. conn.setDoOutput(true);
  21. conn.setRequestProperty("Content-Type","application/soap+xml;charset=utf-8");
  22. conn.setRequestProperty("Content-Length",String.valueOf(data.length));
  23. OutputStreamoutStream=conn.getOutputStream();
  24. outStream.write(data);
  25. outStream.flush();
  26. outStream.close();
  27. if(conn.getResponseCode()==200)
  28. {
  29. returnparseResponseXML(conn.getInputStream());
  30. }
  31. returnnull;
  32. }
  33. /**
  34. *读取Soap文件
  35. *@paraminStream
  36. *@parammobile
  37. *@return
  38. *@throwsException
  39. */
  40. privatestaticStringreadSoapFile(InputStreaminStream,Stringmobile)throwsException
  41. {
  42. //读取输入流
  43. byte[]data=StreamTool.readInputStream(inStream);
  44. Stringsoapxml=newString(data);
  45. Map<String,String>params=newHashMap<String,String>();
  46. params.put("mobile",mobile);
  47. returnreplace(soapxml,params);
  48. }
  49. /**
  50. *替换占位符方法
  51. *@paramxml
  52. *@paramparams
  53. *@return
  54. *@throwsException
  55. */
  56. publicstaticStringreplace(Stringxml,Map<String,String>params)throwsException
  57. {
  58. Stringresult=xml;
  59. if(params!=null&&!params.isEmpty())
  60. {
  61. //循环替换掉所有占位符
  62. for(Map.Entry<String,String>entry:params.entrySet())
  63. {
  64. //需要对$进行转义
  65. Stringname="//$"+entry.getKey();
  66. //使用正则表达式替换
  67. Patternpattern=Pattern.compile(name);
  68. Matchermatcher=pattern.matcher(result);
  69. if(matcher.find())
  70. {
  71. result=matcher.replaceAll(entry.getValue());
  72. }
  73. }
  74. }
  75. returnresult;
  76. }
  77. /**
  78. *解析返回的XML字符串数据
  79. *@paraminStream
  80. *@return
  81. *@throwsException
  82. */
  83. privatestaticStringparseResponseXML(InputStreaminStream)throwsException
  84. {
  85. XmlPullParserparser=Xml.newPullParser();
  86. parser.setInput(inStream,"UTF-8");
  87. //产生第一个事件
  88. inteventType=parser.getEventType();
  89. //只要不是文档结束事件
  90. while(eventType!=XmlPullParser.END_DOCUMENT)
  91. {
  92. switch(eventType)
  93. {
  94. caseXmlPullParser.START_TAG:
  95. //获取解析器当前指向的元素的名称
  96. Stringname=parser.getName();
  97. if("getMobileCodeInfoResult".equals(name))
  98. {
  99. returnparser.nextText();
  100. }
  101. break;
  102. }
  103. eventType=parser.next();
  104. }
  105. returnnull;
  106. }
  107. }

3.工具类

view plain
  1. /**
  2. *上传文件
  3. */
  4. publicclassFormFile
  5. {
  6. /*上传文件的数据*/
  7. privatebyte[]data;
  8. privateInputStreaminStream;
  9. privateFilefile;
  10. /*文件名称*/
  11. privateStringfilname;
  12. /*请求参数名称*/
  13. privateStringparameterName;
  14. /*内容类型*/
  15. privateStringcontentType="application/octet-stream";
  16. publicFormFile(Stringfilname,byte[]data,StringparameterName,StringcontentType)
  17. {
  18. this.data=data;
  19. this.filname=filname;
  20. this.parameterName=parameterName;
  21. if(contentType!=null)this.contentType=contentType;
  22. }
  23. publicFormFile(Stringfilname,Filefile,StringparameterName,StringcontentType)
  24. {
  25. this.filname=filname;
  26. this.parameterName=parameterName;
  27. this.file=file;
  28. try
  29. {
  30. this.inStream=newFileInputStream(file);
  31. }
  32. catch(FileNotFoundExceptione)
  33. {
  34. e.printStackTrace();
  35. }
  36. if(contentType!=null)this.contentType=contentType;
  37. }
  38. publicFilegetFile()
  39. {
  40. returnfile;
  41. }
  42. publicInputStreamgetInStream()
  43. {
  44. returninStream;
  45. }
  46. publicbyte[]getData()
  47. {
  48. returndata;
  49. }
  50. publicStringgetFilname()
  51. {
  52. returnfilname;
  53. }
  54. publicvoidsetFilname(Stringfilname)
  55. {
  56. this.filname=filname;
  57. }
  58. publicStringgetParameterName()
  59. {
  60. returnparameterName;
  61. }
  62. publicvoidsetParameterName(StringparameterName)
  63. {
  64. this.parameterName=parameterName;
  65. }
  66. publicStringgetContentType()
  67. {
  68. returncontentType;
  69. }
  70. publicvoidsetContentType(StringcontentType)
  71. {
  72. this.contentType=contentType;
  73. }
  74. }

view plain
  1. /**
  2. *上传文件
  3. */
  4. publicclassFormFile
  5. {
  6. /*上传文件的数据*/
  7. privatebyte[]data;
  8. privateInputStreaminStream;
  9. privateFilefile;
  10. /*文件名称*/
  11. privateStringfilname;
  12. /*请求参数名称*/
  13. privateStringparameterName;
  14. /*内容类型*/
  15. privateStringcontentType="application/octet-stream";
  16. publicFormFile(Stringfilname,byte[]data,StringparameterName,StringcontentType)
  17. {
  18. this.data=data;
  19. this.filname=filname;
  20. this.parameterName=parameterName;
  21. if(contentType!=null)this.contentType=contentType;
  22. }
  23. publicFormFile(Stringfilname,Filefile,StringparameterName,StringcontentType)
  24. {
  25. this.filname=filname;
  26. this.parameterName=parameterName;
  27. this.file=file;
  28. try
  29. {
  30. this.inStream=newFileInputStream(file);
  31. }
  32. catch(FileNotFoundExceptione)
  33. {
  34. e.printStackTrace();
  35. }
  36. if(contentType!=null)this.contentType=contentType;
  37. }
  38. publicFilegetFile()
  39. {
  40. returnfile;
  41. }
  42. publicInputStreamgetInStream()
  43. {
  44. returninStream;
  45. }
  46. publicbyte[]getData()
  47. {
  48. returndata;
  49. }
  50. publicStringgetFilname()
  51. {
  52. returnfilname;
  53. }
  54. publicvoidsetFilname(Stringfilname)
  55. {
  56. this.filname=filname;
  57. }
  58. publicStringgetParameterName()
  59. {
  60. returnparameterName;
  61. }
  62. publicvoidsetParameterName(StringparameterName)
  63. {
  64. this.parameterName=parameterName;
  65. }
  66. publicStringgetContentType()
  67. {
  68. returncontentType;
  69. }
  70. publicvoidsetContentType(StringcontentType)
  71. {
  72. this.contentType=contentType;
  73. }
  74. }

view plain
  1. publicclassStreamTool
  2. {
  3. /**
  4. *从输入流读取数据
  5. *@paraminStream
  6. *@return
  7. *@throwsException
  8. */
  9. publicstaticbyte[]readInputStream(InputStreaminStream)throwsException
  10. {
  11. ByteArrayOutputStreamoutSteam=newByteArrayOutputStream();
  12. byte[]buffer=newbyte[1024];
  13. intlen=0;
  14. while((len=inStream.read(buffer))!=-1)
  15. {
  16. outSteam.write(buffer,0,len);
  17. }
  18. outSteam.close();
  19. inStream.close();
  20. returnoutSteam.toByteArray();
  21. }
  22. }

4.程序运行效果:

更多相关文章

  1. 【阿里聚安全·安全周刊】一种秘密窃取数据的新型 Android 木马|
  2. 使用adb工具访问sqlite数据库
  3. android 的短信数据库的读取
  4. android应用安全——(数据抓包)跟踪监控android数据包
  5. 我的android 第14天 - 使用SQLiteDatabase操作SQLite数据库

随机推荐

  1. Android(安卓)studio 生成的fragment_mai
  2. android 使用intent传递参数实现乘法计算
  3. android嵌套滑动- Material Design
  4. 申请Android Maps API Key,以及出现的错误
  5. Android Studio 解决方法No JVM installa
  6. Android使用ListView使用
  7. Android Shell命令dumpsys
  8. Android Studio、Android SDK在线更新
  9. Android(安卓)编译错误:unreachable state
  10. 学习Android的Java基础