[Android] Android下实现电话号码归属地的查询
16lz
2021-01-23
需要使用到WebService的服务,这里选择www.webxml.com.cn提供的服务来查询电话号码归属地,使用方法网页上有介绍,这里使用一个实例来演示如何在Android下实现电话号码归属地的查询:
0.使用webxml的soap方式:mobilesoap.xml在src目录下
view plain
- <?xmlversion="1.0"encoding="utf-8"?>
- <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">
- <soap12:Body>
- <getMobileCodeInfoxmlns="http://WebXml.com.cn/">
- <mobileCode>$mobile</mobileCode>
- <userID></userID>
- </getMobileCodeInfo>
- </soap12:Body>
- </soap12:Envelope>
1.MainActivity.java
view plain
- publicclassMainActivityextendsActivity
- {
- privateEditTextmobileText;
- privateTextViewaddressView;
- privatestaticfinalStringTAG="MainActivity";
- @Override
- publicvoidonCreate(BundlesavedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mobileText=(EditText)this.findViewById(R.id.mobile);
- addressView=(TextView)this.findViewById(R.id.address);
- Buttonbutton=(Button)this.findViewById(R.id.button);
- button.setOnClickListener(newView.OnClickListener()
- {
- @Override
- publicvoidonClick(Viewv)
- {
- Stringmobile=mobileText.getText().toString();
- InputStreaminStream=this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml");
- try
- {
- addressView.setText(MobileInfoService.getMobileAddress(inStream,mobile));
- }
- catch(Exceptione)
- {
- Log.e(TAG,e.toString());
- Toast.makeText(MainActivity.this,"查询失败",1).show();
- }
- }
- });
- }
- }
2.MobileInfoService.java
view plain
- publicclassMobileInfoService
- {
- /**
- *获得电话号码归属地信息
- *@paraminStream
- *@parammobile
- *@return
- *@throwsException
- */
- publicstaticStringgetMobileAddress(InputStreaminStream,Stringmobile)throwsException
- {
- //定义输入流
- Stringsoap=readSoapFile(inStream,mobile);
- byte[]data=soap.getBytes();
- URLurl=newURL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
- HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
- conn.setRequestMethod("POST");
- conn.setConnectTimeout(5*1000);
- //如果通过post提交数据,必须设置允许对外输出数据
- conn.setDoOutput(true);
- conn.setRequestProperty("Content-Type","application/soap+xml;charset=utf-8");
- conn.setRequestProperty("Content-Length",String.valueOf(data.length));
- OutputStreamoutStream=conn.getOutputStream();
- outStream.write(data);
- outStream.flush();
- outStream.close();
- if(conn.getResponseCode()==200)
- {
- returnparseResponseXML(conn.getInputStream());
- }
- returnnull;
- }
- /**
- *读取Soap文件
- *@paraminStream
- *@parammobile
- *@return
- *@throwsException
- */
- privatestaticStringreadSoapFile(InputStreaminStream,Stringmobile)throwsException
- {
- //读取输入流
- byte[]data=StreamTool.readInputStream(inStream);
- Stringsoapxml=newString(data);
- Map<String,String>params=newHashMap<String,String>();
- params.put("mobile",mobile);
- returnreplace(soapxml,params);
- }
- /**
- *替换占位符方法
- *@paramxml
- *@paramparams
- *@return
- *@throwsException
- */
- publicstaticStringreplace(Stringxml,Map<String,String>params)throwsException
- {
- Stringresult=xml;
- if(params!=null&&!params.isEmpty())
- {
- //循环替换掉所有占位符
- for(Map.Entry<String,String>entry:params.entrySet())
- {
- //需要对$进行转义
- Stringname="//$"+entry.getKey();
- //使用正则表达式替换
- Patternpattern=Pattern.compile(name);
- Matchermatcher=pattern.matcher(result);
- if(matcher.find())
- {
- result=matcher.replaceAll(entry.getValue());
- }
- }
- }
- returnresult;
- }
- /**
- *解析返回的XML字符串数据
- *@paraminStream
- *@return
- *@throwsException
- */
- privatestaticStringparseResponseXML(InputStreaminStream)throwsException
- {
- XmlPullParserparser=Xml.newPullParser();
- parser.setInput(inStream,"UTF-8");
- //产生第一个事件
- inteventType=parser.getEventType();
- //只要不是文档结束事件
- while(eventType!=XmlPullParser.END_DOCUMENT)
- {
- switch(eventType)
- {
- caseXmlPullParser.START_TAG:
- //获取解析器当前指向的元素的名称
- Stringname=parser.getName();
- if("getMobileCodeInfoResult".equals(name))
- {
- returnparser.nextText();
- }
- break;
- }
- eventType=parser.next();
- }
- returnnull;
- }
- }
3.工具类
view plain
- /**
- *上传文件
- */
- publicclassFormFile
- {
- /*上传文件的数据*/
- privatebyte[]data;
- privateInputStreaminStream;
- privateFilefile;
- /*文件名称*/
- privateStringfilname;
- /*请求参数名称*/
- privateStringparameterName;
- /*内容类型*/
- privateStringcontentType="application/octet-stream";
- publicFormFile(Stringfilname,byte[]data,StringparameterName,StringcontentType)
- {
- this.data=data;
- this.filname=filname;
- this.parameterName=parameterName;
- if(contentType!=null)this.contentType=contentType;
- }
- publicFormFile(Stringfilname,Filefile,StringparameterName,StringcontentType)
- {
- this.filname=filname;
- this.parameterName=parameterName;
- this.file=file;
- try
- {
- this.inStream=newFileInputStream(file);
- }
- catch(FileNotFoundExceptione)
- {
- e.printStackTrace();
- }
- if(contentType!=null)this.contentType=contentType;
- }
- publicFilegetFile()
- {
- returnfile;
- }
- publicInputStreamgetInStream()
- {
- returninStream;
- }
- publicbyte[]getData()
- {
- returndata;
- }
- publicStringgetFilname()
- {
- returnfilname;
- }
- publicvoidsetFilname(Stringfilname)
- {
- this.filname=filname;
- }
- publicStringgetParameterName()
- {
- returnparameterName;
- }
- publicvoidsetParameterName(StringparameterName)
- {
- this.parameterName=parameterName;
- }
- publicStringgetContentType()
- {
- returncontentType;
- }
- publicvoidsetContentType(StringcontentType)
- {
- this.contentType=contentType;
- }
- }
view plain
- /**
- *上传文件
- */
- publicclassFormFile
- {
- /*上传文件的数据*/
- privatebyte[]data;
- privateInputStreaminStream;
- privateFilefile;
- /*文件名称*/
- privateStringfilname;
- /*请求参数名称*/
- privateStringparameterName;
- /*内容类型*/
- privateStringcontentType="application/octet-stream";
- publicFormFile(Stringfilname,byte[]data,StringparameterName,StringcontentType)
- {
- this.data=data;
- this.filname=filname;
- this.parameterName=parameterName;
- if(contentType!=null)this.contentType=contentType;
- }
- publicFormFile(Stringfilname,Filefile,StringparameterName,StringcontentType)
- {
- this.filname=filname;
- this.parameterName=parameterName;
- this.file=file;
- try
- {
- this.inStream=newFileInputStream(file);
- }
- catch(FileNotFoundExceptione)
- {
- e.printStackTrace();
- }
- if(contentType!=null)this.contentType=contentType;
- }
- publicFilegetFile()
- {
- returnfile;
- }
- publicInputStreamgetInStream()
- {
- returninStream;
- }
- publicbyte[]getData()
- {
- returndata;
- }
- publicStringgetFilname()
- {
- returnfilname;
- }
- publicvoidsetFilname(Stringfilname)
- {
- this.filname=filname;
- }
- publicStringgetParameterName()
- {
- returnparameterName;
- }
- publicvoidsetParameterName(StringparameterName)
- {
- this.parameterName=parameterName;
- }
- publicStringgetContentType()
- {
- returncontentType;
- }
- publicvoidsetContentType(StringcontentType)
- {
- this.contentType=contentType;
- }
- }
view plain
- publicclassStreamTool
- {
- /**
- *从输入流读取数据
- *@paraminStream
- *@return
- *@throwsException
- */
- publicstaticbyte[]readInputStream(InputStreaminStream)throwsException
- {
- ByteArrayOutputStreamoutSteam=newByteArrayOutputStream();
- byte[]buffer=newbyte[1024];
- intlen=0;
- while((len=inStream.read(buffer))!=-1)
- {
- outSteam.write(buffer,0,len);
- }
- outSteam.close();
- inStream.close();
- returnoutSteam.toByteArray();
- }
- }
4.程序运行效果:
更多相关文章
- 【阿里聚安全·安全周刊】一种秘密窃取数据的新型 Android 木马|
- 使用adb工具访问sqlite数据库
- android 的短信数据库的读取
- android应用安全——(数据抓包)跟踪监控android数据包
- 我的android 第14天 - 使用SQLiteDatabase操作SQLite数据库