Android下实现电话号码归属地的查询

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

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

<?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>$mobile</mobileCode> <userID></userID> </getMobileCodeInfo> </soap12:Body> </soap12:Envelope>

1.MainActivity.java

public class MainActivity extends Activity { private EditText mobileText; private TextView addressView; private static final String TAG = "MainActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mobileText = (EditText)this.findViewById(R.id.mobile); addressView = (TextView)this.findViewById(R.id.address); Button button = (Button)this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String mobile = mobileText.getText().toString(); InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml"); try { addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile)); } catch (Exception e) { Log.e(TAG, e.toString()); Toast.makeText(MainActivity.this, "查询失败", 1).show(); } } }); } }

2.MobileInfoService.java

public class MobileInfoService { /** * 获得电话号码归属地信息 * @param inStream * @param mobile * @return * @throws Exception */ public static String getMobileAddress(InputStream inStream, String mobile)throws Exception { //定义输入流 String soap = readSoapFile(inStream, mobile); byte[] data = soap.getBytes(); URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); HttpURLConnection conn = (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)); OutputStream outStream = conn.getOutputStream(); outStream.write(data); outStream.flush(); outStream.close(); if(conn.getResponseCode()==200) { return parseResponseXML(conn.getInputStream()); } return null; } /** * 读取Soap文件 * @param inStream * @param mobile * @return * @throws Exception */ private static String readSoapFile(InputStream inStream, String mobile) throws Exception { //读取输入流 byte[] data = StreamTool.readInputStream(inStream); String soapxml = new String(data); Map<String, String> params = new HashMap<String, String>(); params.put("mobile", mobile); return replace(soapxml, params); } /** * 替换占位符方法 * @param xml * @param params * @return * @throws Exception */ public static String replace(String xml, Map<String, String> params)throws Exception { String result = xml; if(params!=null && !params.isEmpty()) { //循环替换掉所有占位符 for(Map.Entry<String, String> entry : params.entrySet()) { //需要对$进行转义 String name = "\\$"+ entry.getKey(); //使用正则表达式替换 Pattern pattern = Pattern.compile(name); Matcher matcher = pattern.matcher(result); if(matcher.find()) { result = matcher.replaceAll(entry.getValue()); } } } return result; } /** * 解析返回的XML字符串数据 * @param inStream * @return * @throws Exception */ private static String parseResponseXML(InputStream inStream) throws Exception { XmlPullParser parser = Xml.newPullParser(); parser.setInput(inStream, "UTF-8"); //产生第一个事件 int eventType = parser.getEventType(); //只要不是文档结束事件 while(eventType!=XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: //获取解析器当前指向的元素的名称 String name = parser.getName(); if("getMobileCodeInfoResult".equals(name)) { return parser.nextText(); } break; } eventType = parser.next(); } return null; } }

3.工具类

/** * 上传文件 */ public class FormFile { /* 上传文件的数据 */ private byte[] data; private InputStream inStream; private File file; /* 文件名称 */ private String filname; /* 请求参数名称*/ private String parameterName; /* 内容类型 */ private String contentType = "application/octet-stream"; public FormFile(String filname, byte[] data, String parameterName, String contentType) { this.data = data; this.filname = filname; this.parameterName = parameterName; if(contentType!=null) this.contentType = contentType; } public FormFile(String filname, File file, String parameterName, String contentType) { this.filname = filname; this.parameterName = parameterName; this.file = file; try { this.inStream = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } if(contentType!=null) this.contentType = contentType; } public File getFile() { return file; } public InputStream getInStream() { return inStream; } public byte[] getData() { return data; } public String getFilname() { return filname; } public void setFilname(String filname) { this.filname = filname; } public String getParameterName() { return parameterName; } public void setParameterName(String parameterName) { this.parameterName = parameterName; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } }

/** * 上传文件 */ public class FormFile { /* 上传文件的数据 */ private byte[] data; private InputStream inStream; private File file; /* 文件名称 */ private String filname; /* 请求参数名称*/ private String parameterName; /* 内容类型 */ private String contentType = "application/octet-stream"; public FormFile(String filname, byte[] data, String parameterName, String contentType) { this.data = data; this.filname = filname; this.parameterName = parameterName; if(contentType!=null) this.contentType = contentType; } public FormFile(String filname, File file, String parameterName, String contentType) { this.filname = filname; this.parameterName = parameterName; this.file = file; try { this.inStream = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } if(contentType!=null) this.contentType = contentType; } public File getFile() { return file; } public InputStream getInStream() { return inStream; } public byte[] getData() { return data; } public String getFilname() { return filname; } public void setFilname(String filname) { this.filname = filname; } public String getParameterName() { return parameterName; } public void setParameterName(String parameterName) { this.parameterName = parameterName; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } }

public class StreamTool { /** * 从输入流读取数据 * @param inStream * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len = inStream.read(buffer)) !=-1 ) { outSteam.write(buffer, 0, len); } outSteam.close(); inStream.close(); return outSteam.toByteArray(); } }

4.程序运行效果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值